ASP.NET MVC 默认路由可通过区域路由访问

发布于 2024-10-10 18:09:15 字数 1247 浏览 6 评论 0原文

到目前为止(为简洁起见),我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日落 2024-10-17 18:09:15

看一下 AreaRegistrationContext.MapRoute 的代码:

public Route MapRoute(string name, string url, object defaults, object constraints, string[] namespaces) {
    if (namespaces == null && Namespaces != null) {
        namespaces = Namespaces.ToArray();
    }

    Route route = Routes.MapRoute(name, url, defaults, constraints, namespaces);
    route.DataTokens["area"] = AreaName;

    // disabling the namespace lookup fallback mechanism keeps this areas from accidentally picking up
    // controllers belonging to other areas
    bool useNamespaceFallback = (namespaces == null || namespaces.Length == 0);
    route.DataTokens["UseNamespaceFallback"] = useNamespaceFallback;

    return route;
}

特别注意 UseNamespaceFallback 标记,它默认设置为 false。如果您想将搜索限制在该区域的命名空间内,则需要具有类似的逻辑。 (True = 在当前命名空间中搜索控制器,如果失败则搜索所有命名空间。False = 仅搜索当前命名空间。)

Take a look at the code for AreaRegistrationContext.MapRoute:

public Route MapRoute(string name, string url, object defaults, object constraints, string[] namespaces) {
    if (namespaces == null && Namespaces != null) {
        namespaces = Namespaces.ToArray();
    }

    Route route = Routes.MapRoute(name, url, defaults, constraints, namespaces);
    route.DataTokens["area"] = AreaName;

    // disabling the namespace lookup fallback mechanism keeps this areas from accidentally picking up
    // controllers belonging to other areas
    bool useNamespaceFallback = (namespaces == null || namespaces.Length == 0);
    route.DataTokens["UseNamespaceFallback"] = useNamespaceFallback;

    return route;
}

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文