ASP.NET MVC Default routes accessible via area routes
Asked Answered
L

1

10

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.

Loupe answered 6/1, 2011 at 6:12 Comment(0)
P
18

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

Ploy answered 6/1, 2011 at 9:6 Comment(1)
Adding "UseNamespaceFallback = false" to the admin area DataTokens dictionary solved the problem. Thanks!Loupe

© 2022 - 2024 — McMap. All rights reserved.