开发者

ASP.NET MVC Default routes accessible via area routes

开发者 https://www.devze.com 2023-02-02 23:50 出处:网络
So far (for brevity) I have one route in global.asax registered like this: routes.Add(new LowercaseRoute(\"{action}/{id}\", new MvcRouteHandler())

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开发者_Go百科 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消