Can I get the current executing controller from HttpContext?

﹥>﹥吖頭↗ 提交于 2019-12-04 19:32:23

问题


I am using some 3rd party classes. I want to get the routevalues of my controller in that. Unfortunately it doesn't hand me the current controller that is executing. Can I get it from HttpContext?

The class looks something like:

public class ServiceStationVisibilityProvider
        : ISiteMapNodeVisibilityProvider 
    {

        public bool IsVisible(SiteMapNode node, HttpContext context, IDictionary<string, object> sourceMetadata)
        {
            node.Title = DateTime.Now.ToString(); //need to access routevalues and set title
            return true;
        }

Now I could manully inspect Request.RawUrl and parse and do funky things. However, I don't want to write that kind and fall into trouble later when the application grows. }


回答1:


You can search the values of "controller" and "action" in this object

HttpContext.Request.RequestContext.RouteData.Values



回答2:


Not sure in what context you are executing, but you can get it from the RequestContext:

RequestContext.RouteData.Values["controller"].ToString()


来源:https://stackoverflow.com/questions/7684532/can-i-get-the-current-executing-controller-from-httpcontext

工具导航Map