MVC路由选择根区域控制器(MVC Routing picking up Area controllers at root)

编程入门 行业动态 更新时间:2024-10-21 18:52:57
MVC路由选择根区域控制器(MVC Routing picking up Area controllers at root)

我在我的控制器遇到困难,这些控制器在一个区域接听不在该区域的路线请求。 所以我有这样的设置(额外的东西切割):

/Areas/Security/Controllers/MembersController.cs /Areas/Security/SecurityAreaRegistration.cs /Controllers/HomeController.cs

我有我的安全区域定义:

namespace MyApp.Web.Areas.Security { public class SecurityAreaRegistration : AreaRegistration { public override string AreaName { get { return "Security"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Security_default", "Security/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } } }

和我的全球路由规则:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{*robotstxt}", new { robotstxt = @"(.*/)?robots.txt(/.*)?" }); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "MyApp.Web.Controllers" } );

在我的全球asax中,我做了很多事情,但相关的部分是我调用了AreaRegistration.RegisterAllAreas(); 然后我调用上述的路由功能。

但我的问题是,“/ Members /”的请求正在使用我的“默认”路由命中我的成员控制器...即使控制器不在我指定的名称空间中。 然后,当它试图运行它时,无法找到它的Views,因为它们是在Area中定义的,它试图在整个Views文件夹中找到它们。 我尝试制作路由命名空间"Weird.Namespace.With.No.Content"并且它仍然命中成员控制器 - 我找不到任何方法使它不使用该控制器。 如何让它不回答不在其区域内的请求?

I'm having difficulties with my controllers that are in an Area answering requests on routes that aren't for the area. So I have a setup like this (Extra stuff cut):

/Areas/Security/Controllers/MembersController.cs /Areas/Security/SecurityAreaRegistration.cs /Controllers/HomeController.cs

I have my area for security defined:

namespace MyApp.Web.Areas.Security { public class SecurityAreaRegistration : AreaRegistration { public override string AreaName { get { return "Security"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Security_default", "Security/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } } }

And my global routing rules:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{*robotstxt}", new { robotstxt = @"(.*/)?robots.txt(/.*)?" }); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "MyApp.Web.Controllers" } );

In my global asax I'm doing quite a few things but the relevant part is that I call AreaRegistration.RegisterAllAreas(); then I call the routing function that does the above.

But my problem is that requests for "/Members/" are hitting my Members controller using my "Default" route... even though the controller's not in the namespace I specified. Then when it tries to run it can't find it's Views cause they're defined in the Area and it's trying to find them in the overall Views folders. I tried making the route namespace "Weird.Namespace.With.No.Content" and it STILL hits the Members controller - I can't find any way to make it not use that controller. How do I make it not answer requests that aren't in it's area?

最满意答案

通过将路线更改为:找到解决方案:

routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "MyApp.Web.Controllers" } ).DataTokens["UseNamespaceFallback"] = false;

出于某种原因,未设置它似乎始终找到我的控制器,无论他们在哪里,完全忽略我的命名空间 - 甚至从其他引用的程序集。 通过查看默认控件的ILSpyFactory,如果找不到你要求的名称空间中的控制器,看起来GetControllerType最终会回退到绝对每个控制器的搜索...

这个标志似乎是在我在特定区域制作的路由上自动设置的,而不是我在全局制作的路径上设置的。 当我将其设置在全球范围内时,他们开始表现出我最初的预期。 我不知道你为什么要打开这个...

Ended up finding a solution by changing the Route to:

routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "MyApp.Web.Controllers" } ).DataTokens["UseNamespaceFallback"] = false;

For some reason with that unset it seemed to always find my Controllers no matter where they were and totally disregard my namespaces - even from other referenced assemblies. Looking through the ILSpy of DefaultControllerFactory it looks like GetControllerType eventually falls back to searching absolutely every controller if it doesn't find the controller in the namespaces you asked for...

This flag seems to be set automatically on the Routes I made in a specific area, but not on the ones I made globally. When I set it on the global ones they began behaving how I had originally expected. I have no idea why you'd ever want to turn this on...

更多推荐

本文发布于:2023-08-01 02:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   控制器   区域   MVC   Routing

发布评论

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

>www.elefans.com

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