In MVC3, how to get the current action name?

瘦欲@ 提交于 2019-12-19 07:40:35

问题


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();
        }
    }
}

回答1:


var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
if (routeValues != null) 
{
    if (routeValues.ContainsKey("action"))
    {
        var actionName = routeValues["action"].ToString();
    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/20626860/in-mvc3-how-to-get-the-current-action-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!