سؤال

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();
        }
    }
}
هل كانت مفيدة؟

المحلول

var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
if (routeValues != null) 
{
    if (routeValues.ContainsKey("action"))
    {
        var actionName = routeValues["action"].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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top