在过滤器属性中获取区域,控制器和动作名称

编程入门 行业动态 更新时间:2024-10-10 08:21:34
本文介绍了在过滤器属性中获取区域,控制器和动作名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我具有全局属性,需要在其中知道区域,控制器和动作.由于路由(内部区域注册和属性路由),我无法使用RawUrl.我遵循以下两种方法,但是在两种情况下,我的区域都返回为空.当我使用路线时,我会得到区域名称.当我执行redirecttoaction或url.action或手动键入url等时,为什么我的区域为null?我们正在使用MVC 5.0.

I have global attribute in which i need to know the area, controller and action. I can't use RawUrl due to routes (inside area registration and attribute routing). I have followed the following two approaches but in both cases, my area is coming back as null. When i use a route then i get the area name just fine. Why is my area null when i do redirecttoaction or url.action or type the url manually etc? We are using MVC 5.0.

var routingValues = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)).Values; var currentArea = (string) routingValues["area"] ?? string.Empty; var currentController = (string) routingValues["controller"] ?? string.Empty; var currentAction = (string) routingValues["action"] ?? string.Empty;

2:

var handler = HttpContext.Current.Handler as System.Web.Mvc.MvcHandler; var area = handler.RequestContext.RouteData.Values["area"]; var controller = handler.RequestContext.RouteData.Values["controller"]; var action = handler.RequestContext.RouteData.Values["action"];

就像我上面说的那样,如果我使用路线(/TipHotLine),那么我得到的区域名称就很好了.

Like i said above, if i use a route (/TipHotLine) then i get the area name just fine.

public class AgencyAreaRegistration : AreaRegistration { public override string AreaName { get { return "Agency"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Agency_default", "Agency/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); context.MapRoute( "tiphotline", "tiphotline", new { controller = "tiphotline", action = "Index", Area = "Agency" } ); } }

推荐答案

注册区域后,使用的MapRoute方法将dataContextToken添加到每个路由.您可以在此处检查源代码,将会看到如下所示的方法,并且您会注意到一行添加了数据令牌:

When an area is registered the MapRoute method used is adding a dataContextToken to each route. You can check the source code here, you will see a method like the following and you will notice a line adding the data token:

public Route MapRoute(string name, string url, object defaults, object constraints, string[] namespaces) { ... route.DataTokens[RouteDataTokenKeys.Area] = AreaName; ... return route; }

因此,在过滤器中,您只需要使用键"area"而不是路由值来获取数据令牌.例如,以下过滤器将在路由中找到的区域,控制器和动作中添加标题

So in your filter you just need to get the data token with key "area" instead of a route value. For example the following filter will add a header the area, controller, and action found in the route

public override void OnActionExecuting(ActionExecutingContext filterContext) { var routingValues = filterContext.RouteData.Values; var currentArea = filterContext.RouteData.DataTokens["area"] ?? string.Empty; var currentController = (string)routingValues["controller"] ?? string.Empty; var currentAction = (string)routingValues["action"] ?? string.Empty; filterContext.HttpContext.Response.AddHeader("Routing info", string.Format("controller={0},action={1},area={2}", currentController, currentAction, currentArea)); }

更多推荐

在过滤器属性中获取区域,控制器和动作名称

本文发布于:2023-11-17 11:15:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   控制器   属性   区域   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!