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

11,885

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;
}
Share:
11,885
rksprst
Author by

rksprst

Updated on June 04, 2022

Comments

  • rksprst
    rksprst almost 2 years

    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.

  • Jim Schubert
    Jim Schubert almost 15 years
    maybe try the above on: System.Web.Abstractions, System.Web.Extensions, System.Web.Mvc, and System.Web.Routing? I think those are the four references used by asp.net.
  • rksprst
    rksprst almost 15 years
    When I republished, I did "copy local" on all the MVC related dlls... so those plus some other regular asp.net dlls just in case. I just don't see why it would work on dev and not on staging server.