In MVC3, how to get the current action name?

Is there a way to use an HttpContext or View context to get the current action name?

I can get the name of the controller using

    var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;

    if (routeValues != null) 
    {
        if (routeValues.ContainsKey("controller"))
        {
            controllerName = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
        }
    }
}
+4
source share
2 answers
var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
if (routeValues != null) 
{
    if (routeValues.ContainsKey("action"))
    {
        var actionName = routeValues["action"].ToString();
    }
}
+15
source
ViewContext.RouteData.Values["action"]

As far as I know, ViewContext.RouteData.Values ​​will never be null and will always have the keys "controller" and "action". Please correct me if I am wrong.

0
source

All Articles