Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewContext.RouteData.Values["action"] is null on server... works fine on local machine

I'm having a weird issue where ViewContext.RouteData.Values["action"] is null on my staging server, but works fine on my dev machine (asp.net development server).

The code is simple:

public string CheckActiveClass(string actionName)
    {
        string text = "";
        if (ViewContext.RouteData.Values["action"].ToString() == actionName)
        {
            text = "selected";
        }
        return text;
    }

I get the error on the ViewContext.RouteData.Values["action"] line. The error is:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Any help is appreciated. Thanks in advance.

like image 946
rksprst Avatar asked Nov 05 '22 20:11

rksprst


1 Answers

Do you have different versions of asp.net mvc on your dev and staging servers? Try copying System.Web.Mvc locally to the staging server and see if that fixes it. (Right click on the reference, choose properties, and change Copy Local to true)

This may or may not help your situation, but here is a helper extension I stole from an MVC template on asp.net/mvc:

/// <summary>
/// Checks the current action via RouteData
/// </summary>
/// <param name="helper">The HtmlHelper object to extend</param>
/// <param name="actionName">The Action</param>
/// <param name="controllerName">The Controller</param>
/// <returns>Boolean</returns>
public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)
{
    string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
    string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

    if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
        return true;

    return false;
}
like image 125
Jim Schubert Avatar answered Nov 12 '22 20:11

Jim Schubert