问题描述
我具有全局属性,需要在其中知道区域,控制器和动作.由于路由(内部区域注册和属性路由),我无法使用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));
}
这篇关于在过滤器属性中获取区域,控制器和动作名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!