我在我的控制器遇到困难,这些控制器在一个区域接听不在该区域的路线请求。 所以我有这样的设置(额外的东西切割):
/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.csI 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...
更多推荐
发布评论