In MVC3, how to get the current action name?

后端 未结 2 1724
天命终不由人
天命终不由人 2021-01-13 01:30

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 = Http         


        
相关标签:
2条回答
  • 2021-01-13 02:11
    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 讨论(0)
  • 2021-01-13 02:20
    var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
    if (routeValues != null) 
    {
        if (routeValues.ContainsKey("action"))
        {
            var actionName = routeValues["action"].ToString();
        }
    }
    
    0 讨论(0)
提交回复
热议问题