var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
if (routeValues != null)
{
if (routeValues.ContainsKey("action"))
{
var actionName = routeValues["action"].ToString();
}
}
In MVC3, how to get the current action name?
-
02-09-2022 - |
سؤال
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();
}
}
}
المحلول
نصائح أخرى
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.
لا تنتمي إلى StackOverflow