ASP.NET MVC 默认路由可通过区域路由访问
到目前为止(为简洁起见),我在 global.asax 中注册了一条路由,如下所示:
routes.Add(new LowercaseRoute("{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
DataTokens = rootNamespace
});
其中“rootNamespace”为
var rootNamespace = new RouteValueDictionary(new { namespaces = new[] { "MyApp.Web.Controllers" } });
LowercaseRoute 继承自 Route,并使所有路径小写。我也有一个这样注册的区域:
context.Routes.Add(new LowercaseRoute("admin/{controller}/{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { action = "List", id = UrlParameter.Optional }),
DataTokens = adminNamespace
});
其中 adminNamespace 是另一个命名空间,与默认路由中的想法相同,但具有正确的命名空间。这工作正常,我可以访问如下所示的 URL:
http://example.com/contact <- default route, "Home" controller
http://example.com/admin/account <- area route, "Account" controller, default "List" action
问题是这
http://example.com/admin/home/contact
也能工作。在“管理”区域下没有具有“联系”操作的“主”控制器。它从“/contact”中提取正确的页面,但 URL 为“/admin/home/contact”。
有什么办法可以防止这种情况发生吗?
谢谢。
So far (for brevity) I have one route in global.asax registered like this:
routes.Add(new LowercaseRoute("{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
DataTokens = rootNamespace
});
Where "rootNamespace" is
var rootNamespace = new RouteValueDictionary(new { namespaces = new[] { "MyApp.Web.Controllers" } });
LowercaseRoute inherits from Route and just makes all paths lowercase. I also have an area registered like this:
context.Routes.Add(new LowercaseRoute("admin/{controller}/{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { action = "List", id = UrlParameter.Optional }),
DataTokens = adminNamespace
});
Where adminNamespace is another namespace, same idea as in the default route, but with the right namespace. This works fine, I can access URLs that look like this:
http://example.com/contact <- default route, "Home" controller
http://example.com/admin/account <- area route, "Account" controller, default "List" action
The problem is that this
http://example.com/admin/home/contact
also works. There's no "home" controller with a "contact" action under the "admin" area. It pulls the right page from "/contact" but with URL being "/admin/home/contact".
Is there any way to prevent this from happening?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 AreaRegistrationContext.MapRoute 的代码:
特别注意 UseNamespaceFallback 标记,它默认设置为 false。如果您想将搜索限制在该区域的命名空间内,则需要具有类似的逻辑。 (True = 在当前命名空间中搜索控制器,如果失败则搜索所有命名空间。False = 仅搜索当前命名空间。)
Take a look at the code for AreaRegistrationContext.MapRoute:
Note in particular the UseNamespaceFallback token, which is set to false by default. You need to have similar logic if you want to limit the search to the area's namespace. (True = search the current namespace for the controller, and failing that search all namespaces. False = search the current namespace only.)