ASP.NET MVC缺省路由通过区域内路由访问 [英] ASP.NET MVC Default routes accessible via area routes

查看:108
本文介绍了ASP.NET MVC缺省路由通过区域内路由访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止(为简单起见)我在注册这样的Global.asax一条路线:

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
  }); 

在哪里rootNamespace是

Where "rootNamespace" is

var rootNamespace = new RouteValueDictionary(new { namespaces = new[] { "MyApp.Web.Controllers" } });

LowercaseRoute从路线的继承和只是让小写的所有路径。我也注册了一个这样的区域:

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
  });

在哪里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

的问题是,这

http://example.com/admin/home/contact

也有效。有没有家控制器下的管理区域中的接触的动作。它从/接触正确的网页,但与URL为/管理/首页/联系。

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

有什么办法prevent这种情况发生?

Is there any way to prevent this from happening?

感谢。

推荐答案

在code为AreaRegistrationContext.MapRoute请看下图:

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;
}

请注意特别的 UseNamespaceFallback 的标记,这是默认设置为false。你需要有类似的逻辑,如果你想将搜索限制该地区的命名空间。 (真=搜索当前的命名空间为控制器,否则要求搜索所有的命名空间。FALSE =仅搜索当前的命名空间。)

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

这篇关于ASP.NET MVC缺省路由通过区域内路由访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆