Fixing the Two Home Controller Problem

Has Microsoft MVC ever complained to you about not being able to load your site’s home page due to having two home controllers? Sometimes this can happen after you publish your site after some refactoring.

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:
MvcApplication1.Controllers.HomeController
MvcApplication1.Controllers.HomeController

MultipleHomeControllersErrorPage

The first time it happened to me I searched my solution for more than one controller named HomeController, verified I had no other ‘Areas’ defined with a second home controller in it, and then was baffled that there was only one file in the entire solution called HomeController. Yet the error clearly says there are two controllers of the same name.



The Solution

Given that the error started after a publish that should have given a clue as to why it happened. Remember how IIS web applications work. All the dlls are loaded into the AppDomain. When MVC needs to resolve a route and load a controller it uses convention to find a controller meeting the naming convention. It expects to only find the controller in one dll, but it checks what MVC found at application startup for a match. If you define a controller of the same name in two assemblies then it complains with this error.

Remember I said this happens after a refactoring. I had refactored the project name and the corresponding assembly name and default namespace of the application. In the case above I did not yet refactor all classes (like the controller) in that base namespace change. But even if I had you’d still see two home controllers, but one for each the old and new namespace. Any ideas yet?

When you publish a web application the default behavior is to publish all files needed to run the application. Another default is not to delete all files before publish, and instead merge the newest files into the existing folder. So if you renamed the assembly, the old one is not deleted on publish. So both the old and new MVC application dll is loaded into the AppDomain. The simple solution is to delete the old dll from the bin folder of the MVC application, or when you publish tell it to delete all files before the publish.

About the Author

Michael Lang

Co-Founder and CTO of Watchdog Creative, business development, technology vision, and more since 2013, Developer, and Mentor since 1999. See the about page for more details.

4 Comments

  • By Shoneye Tunde

    thanks i got it fixed in from the dll

  • By Mehmet

    This is the best solution for tihs problem thank you.

  • By Siva

    Thanks alot