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

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