简体   繁体   English

我可以从HttpContext获取当前正在执行的控制器吗?

[英]Can I get the current executing controller from HttpContext?

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? 我可以从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. 现在我可以手动检查Request.RawUrl并解析并做一些时髦的事情。 However, I don't want to write that kind and fall into trouble later when the application grows. 但是,我不想写那种,并在应用程序增长后遇到麻烦。 } }

您可以在此对象中搜索"controller""action"的值

HttpContext.Request.RequestContext.RouteData.Values

不确定您正在执行的上下文,但您可以从RequestContext获取它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 SeriLog Sink 中获取当前的 HttpContext? - How can I get the current HttpContext in a SeriLog Sink? 为什么我不能从Controller初始化程序访问HttpContext? - Why can't I access the HttpContext from the Controller initializer? ASP.NET Core 3.1,从当前HttpContext中的referrer url获取controller和动作名称 - ASP.NET Core 3.1, get the controller and action name from referrer url in the current HttpContext 从 httpcontext do.net 6 获取 controller 名称 - Get controller name from httpcontext dotnet 6 In.Net Core,如何在 HttpContext 的 controller 中设置一个变量以用于所有 controller 方法? - In .Net Core, how can I set a variable in a controller from HttpContext to use in all of the controller methods? 如何在不使用HttpContext的情况下获取Web应用程序的当前配置? - How can I get current config of the web application without using HttpContext? 如何获取超过1000个html控件值的HttpContext.Current.Request? - how can i get HttpContext.Current.Request of more than 1000 html controls value? 如何获取为当前正在执行的httphandler配置的路径? - How can i get the path configured for the current executing httphandler? 从 HttpContext 获取当前 System.Web.UI.Page? - Get current System.Web.UI.Page from HttpContext? 我可以使用动态重定向HttpContext.Current.Request吗? - can I use dynamic to redirect HttpContext.Current.Request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM