Question

I have an MVC application with a couple of areas. Each of the areas, as well as the root, has a HomeController.

/Home/Index
/Admin/Home/Index
/Content/Home/Index

All of these work fine and properly determine which home controller to use.

My problem is that each of the areas also has ReportsController, but the root does not.

/Admin/Reports/Index
/Content/Reports/Index

Both of these work as expected, but I get the "Multiple types were found that match the controller" error for the url /Reports/Index. It's matching on the default route but spilling over to the areas. My assumption would be that it should 404, the same way going to a route missing from all areas does.

My routes are configured as follows:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new[] { "Website.Controllers" }
);

context.MapRoute(
    "Content",
    "Content/{controller}/{action}/{id}",
    new { action = "Index", controller = "Home", id = UrlParameter.Optional },
    namespaces: new[] { "Website.Areas.Content.Controllers" }
);

context.MapRoute(
    "Admin",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", controller = "Home", id = UrlParameter.Optional },
    namespaces: new[] { "Website.Areas.Admin.Controllers" }
);

The problem route (/Reports/Index) is matching on the Default route map, but rather than being limited to the defined namespace of Website.Controllers it's also looking in the areas. Instead, it should limit its search to Website.Controllers and return 404 when it doesn't find a ReportsController there.

Was it helpful?

Solution

As outlined in the following post, namespaces are for prioritization, but can be made restrictive by disabling the fallback lookup as follows:

routes.MapRoute(...).DataTokens["UseNamespaceFallback"] = false;

http://bubblogging.wordpress.com/2012/06/09/mvc-routing-namespaces/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top