Back to blog

Fixing ‘multiple types were found that match the controller named’

October 12, 2010 - Posted in ASP.NET MVC Posted by:

ASP.NET MVC introduced the concept of ‘areas’ within a web app.
They’re a great way to split a large app into different sections that handle different logic, for example you might have an admin area that has some authorization and handles all of the back end content management for the app. You can think of them almost as ‘sub-projects’, as they have their own controllers and views.

It isn’t unlikely that you might have the default controller called Home in the root, and also another controller called Home in a separate area.
However, because of the fact that MVC works as pre-compiled web application (rather than a web site), all the controllers get compiled into the assembly and you get the following error:

Multiple types were found that match the controller named ‘Home’. This can happen if the route that services this request (‘{controller}/{action}/{id}’) does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the ‘MapRoute’ method that takes a ‘namespaces’ parameter.




The request for ‘Home’ has found the following matching controllers:
EdibleCode.Web.Controllers.HomeController
EdibleCode.Web.Areas.Admin.Controllers.HomeController

So, despite the fact that the controllers are in different namespaces, the app doesn’t know which one to use. Fortunately, there is an easy fix to tell MVC which namespace to use as the default. Open the global.asax.cs file from the root, and find the Application_Start method. It should have two lines in at the moment (AreaRegistration.RegisterAllAreas(); and RegisterRoutes(RouteTable.Routes);). Simply add the following line after these, replacing the namespace in quotes with the namespace of your controllers:


ControllerBuilder.Current.DefaultNamespaces.Add("EdibleCode.Controllers");

16 Comments

Uday Shastri 3 years ago

Great!!!
Saved my lot of time.

Muhammad 3 years ago

Thnx bro ,, It really helped me to solve the routing issue .

streetspirit 3 years ago

Thx man, gotcha

Rich Hosek 3 years ago

Okay, but then when I try to use the following route:

domain.com/AreaName

I do not get my Home/Index controller/action even though I have

context.MapRoute(
"AreaName_default",
"AreaName/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);

in my AreaNameAreaRegistration.cs file.

Ian Routledge 3 years ago

Hi Rich, try

context.MapRoute(
"AreaName_default",
"AreaName/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Aaron Freeland 3 years ago

Worked great…much appreciated =)

Tom Wayson 3 years ago

Thanks. Solved my problem (FYI – on MVC 3).

Saeed Neamati 3 years ago

I got this error because I changed the namespace of the entire project. However, I don’t have any areas, and there is really no conflict in areas here. What could be the reason?

Ian Routledge 3 years ago

Hi Saeed

This will be because the old compiled dll will still be in the bin folder so the runtime will be picking up both the new dll and the old dll. Simply clean the solution in visual studio or manually go in the bin folder and delete the old dll.

Ian

Andrej 3 years ago

Worked like a charm. Thanks a lot, man.

Jon 3 years ago

Nice tip, and I was on the point of using your advice when I found something similar at http://stackoverflow.com/questions/5092589/having-issue-with-multiple-controllers-of-the-same-name-in-my-project
- you can also specify a namespaces “search list” in the route itself. I had the same duplicate “Home” controller issue but solved it with:

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string [] { "My_App_Name.Controllers" }
);

Jon

Ian Routledge 3 years ago

Ah yes, so you can. Good stuff, thanks for contributing.

Leonard Iilonga 2 years ago

Worked perfectly. Thanks a million.

Hatef 2 years ago

Thank you very much ..

Adam 2 years ago

This fixed my problem, thank you for taking the time to write this blog article. Legend :D

Navtej Singh 2 years ago

Saved a lot of time…
gOOd information