In MVC3, how to get the current action name?

13,300
var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
if (routeValues != null) 
{
    if (routeValues.ContainsKey("action"))
    {
        var actionName = routeValues["action"].ToString();
    }
}
Share:
13,300
Chris Ballance
Author by

Chris Ballance

Senior Engineering manager at Mark43 Señor Software Developer at Diligent Microsoft Alumni Code to Share Photographer BSC, MCP, CSM@ballancefacebookstudent

Updated on June 04, 2022

Comments

  • Chris Ballance
    Chris Ballance almost 2 years

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

    I can get the controller name 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();
            }
        }
    }
    
  • Chris Ballance
    Chris Ballance over 5 years
    Thanks for the simplification, @PeterB!