Areas and ASP.NET MVC Routes Tip

Lets say that you have several areas define in your ASP.NET MVC solution. You find that when you run your application you get the following error “Multiple types were found that match the controller named ‘Home’.”. This is because ASP.NET MVC finds all the routing definitions (i.e in each area.cs file or the global.asax) and sees conflicts with duplicate controller names.
Simple solution is to associate the namespace with the routes you are registering. Take the example below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// global.asax route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "MyApplication.Controllers" } // Controller Namespace
);
?
// Area Registration Route
context.MapRoute(
MyArea_default,
"MyArea/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "MyApplication.Areas.MyArea.Controllers" }
);

Credit to Steve Testa, Thanks!.