Multiple types were found that match the controller named 'Home'

195,254

Solution 1

This error message often happens when you use areas and you have the same controller name inside the area and the root. For example you have the two:

  • ~/Controllers/HomeController.cs
  • ~/Areas/Admin/Controllers/HomeController.cs

In order to resolve this issue (as the error message suggests you), you could use namespaces when declaring your routes. So in the main route definition in Global.asax:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] { "AppName.Controllers" }
);

and in your ~/Areas/Admin/AdminAreaRegistration.cs:

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

If you are not using areas it seems that your both applications are hosted inside the same ASP.NET application and conflicts occur because you have the same controllers defined in different namespaces. You will have to configure IIS to host those two as separate ASP.NET applications if you want to avoid such kind of conflicts. Ask your hosting provider for this if you don't have access to the server.

Solution 2

Here is another scenario where you might confront this error. If you rename your project so that the file name of the assembly changes, it's possible for you to have two versions of your ASP.NET assembly, which will reproduce this error.

The solution is to go to your bin folder and delete the old dlls. (I tried "Rebuild Project", but that didn't delete 'em, so do make sure to check bin to ensure they're gone)

Solution 3

In MVC4 & MVC5 It is little bit different, use following

/App_Start/RouteConfig.cs

namespace MyNamespace
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

and in Areas

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

Solution 4

Watch this... http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2-areas

Then this picture (hope u like my drawings)

enter image description here

Solution 5

in your project bin/ folder

make sure that you have only your PROJECT_PACKAGENAME.DLL

and remove ANOTHER_PROJECT_PACKAGENAME.DLL

that might appear here by mistake or you just rename your project

Share:
195,254
Only Bolivian Here
Author by

Only Bolivian Here

Updated on July 08, 2022

Comments

  • Only Bolivian Here
    Only Bolivian Here almost 2 years

    I currently have two unrelated MVC3 projects hosted online.

    One works fine, the other doesn't work, giving me the 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 way my hoster works is that he gives me FTP access and in that folder I have two other folder, one for each of my applications.

    ftpFolderA2/foo.com

    ftpFolderA2/bar.com

    foo.com works fine, I publish my application to my local file system then FTP the contents and it works.

    When I upload and try to run bar.com, the issue above fires and prevents me from using my site. All while foo.com still works.

    Is bar.com searching from controllers EVERYWHERE inside of ftpFolderA2 and that's why it's finding another HomeController? How can I tell it to only look in the Controller folder as it should?

    Facts:

    1. Not using areas. These are two COMPLETELY unrelated projects. I place each published project into each respective folder. Nothing fancy.
    2. Each project only has 1 HomeController.

    Can someone confirm this is the problem?

  • Only Bolivian Here
    Only Bolivian Here over 12 years
    I'm not using areas at all. These are two completely unrelated applications residing in separate folder inside an FTP root folder. Maybe my application is looking for MVC controllers everywhere it can and that reach just so happens to extend to the other Home Controller. How can I tell it to not look anywhere but it's own Controller folder and disregard the rest?
  • Darin Dimitrov
    Darin Dimitrov over 12 years
    @SergioTapia, it seems that they are pretty related your applications. Your hosting provider put them inside the same ASP.NET application. You will have to ask him split them in IIS as separate instances or you will have lots of problems.
  • Don McCauley
    Don McCauley almost 12 years
    Thanks this is simple and does the job.
  • om471987
    om471987 over 11 years
    Thanks. In ASP MVC 4.0 you need to pass named argument like namespaces: new[] {"AppName.Areas.Admin.Controllers" }
  • Travis J
    Travis J almost 11 years
    +1 - Works well. I didn't realize there was a separate area for route registration in areas. Everywhere I look it seems there is a quality answer from Darin :)
  • Aruna
    Aruna about 10 years
    Solved the issue..! :)
  • Paul Zahra
    Paul Zahra over 9 years
    This bit me when copying a project and renaming it... the old project named dll was still in the bin, a cleanup didn't remove it... I had to manually delete it!
  • Abhimanyu
    Abhimanyu about 9 years
    your suggestion saved me
  • katesky8
    katesky8 about 9 years
    me too, thanks , clean dosnt actually mean clean , grrrr
  • keithl8041
    keithl8041 over 8 years
    Is there a way of doing something similar in Web Api 2? The fourth argument for the Routes.MapHttpRoute only takes constraints and doesn't understand the namespace argument.
  • Form
    Form about 8 years
    This was the fix for me. My publish profile did not remove files not present locally, so my app picked up old dlls in addition to new ones and found duplicate types.
  • Sebastian 506563
    Sebastian 506563 about 8 years
    Other variation of this error is when you use resharper and you use some "auto" refactor options that include namespace name changing. This was what happen to me.
  • Adriaan Davel
    Adriaan Davel about 8 years
    This happened to me when I copied to contents of 1 project over the contents of another. I had to delete the specific files from the bin folder
  • Mauro Valvano
    Mauro Valvano about 8 years
    I changed my project name and i refractored all the files but then i got this error. Deleting the bin folder worked for me too.
  • Tom Blodget
    Tom Blodget almost 8 years
    If you are getting this from an Azure App Service, go to https://<your_app_name_here>.scm.azurewebsites.net/DebugCons‌​ole to log in and delete files.
  • DanKodi
    DanKodi almost 8 years
    Yes It's not enough to just provide a namespace in MapRoute. The namespace provided here need to match the namespace of the controller class resides in. Now it works!
  • brando
    brando almost 8 years
    Thx this was the problem for me. I'd created a "new" project by copy/pasting an existing project into a new folder; the old build dlls came with, deleting bin folder wiped it clean
  • Karthic G
    Karthic G over 7 years
    hi, I've tried yours code, but it comes an error again The request for 'Health' has found the following matching controllers: Lovefreerangeeggs.Areas.Admin.Controllers.HealthController Lovefreerangeeggs.Controllers.HealthController, What should I do?
  • Roberto Bonini
    Roberto Bonini over 7 years
    I got this when moving my project files to a second drive. Clearing out the bin folder solves it. Oddest darn thing.
  • Troy Grosfield
    Troy Grosfield about 7 years
    Well that was a painfully annoying error with a very simple fix. Thanks!
  • Ravi Anand
    Ravi Anand about 7 years
    great solution if you have same controllers in multiple projects
  • Jason Beck
    Jason Beck about 7 years
    This was the case for me. If you truly have multiple controllers with the same name, this may be needed after you've added namespaces to your route definitions. For example, for your homepage where the controller and area aren't explicitly chosen by the path.
  • Patrick Borkowicz
    Patrick Borkowicz almost 7 years
    This occured for me when I had an OData controller and an MVC controller with the same name (different namespaces), except in my case the error message also said different namespaces are not supported for OData routes, and indeed, the OData route config does not accept a namespaces parameter like WebAPI config does. The error also ONLY OCCURED ON MY AZURE PRODUCTION SERVER, after combining code from a couple of projects. To fix the issue, I used FTP to connect to the Azure site, cleared the root dir, and redeployed.
  • AnotherDeveloper
    AnotherDeveloper almost 7 years
    @TomBlodget Thank you. This was what I needed. I was able to set a delete previous files option on the publishing profile, but basically, it's the same as your solution.
  • Gavin Ward
    Gavin Ward almost 7 years
    If you are using areas and want to namespace the controllers, you need to namespace both the routes inside the area and outside. Only namespacing the area route still gave me this issue.
  • Mike
    Mike almost 7 years
    Thank you @DrTJ This was soooooo frustrating! You expect the dang clean process to work and expectations is the root of failure. This saved me pulling out my hair further!
  • Satish Kumar sonker
    Satish Kumar sonker over 6 years
    i was facing the similar issue. and main reason was that i had the same controller in two different Area
  • Mavi Domates
    Mavi Domates over 6 years
    same here this was the case for me. Thanks a lot!
  • Derreck Dean
    Derreck Dean about 6 years
    In the project I work on, we have a main turnkey backoffice with areas for custom client work. Each one has a 'settings' controller. This answer is a great alternative to having to define a route for the settings controller for each area.
  • Yeronimo
    Yeronimo almost 6 years
    This was the problem for me. A colleague added by mistake a reference from one front-end project to another creating this issue. He removed the reference, thus Visual Studio also removing the dll files on his disk. I pulled the update from Git, references were gone, but the dll files remained, evenafter a clean. Simply because my VS didn't see the reference anymore. But when running IIS saw the files and used them. Removing them from my disk helped.
  • Saravanan Sachi
    Saravanan Sachi over 5 years
    Seems Clean and Rebuild don't clear bin folder. Manually we have to delete.
  • Detilium
    Detilium over 5 years
    Exactly my issue. Thank you.
  • Carlos Muñoz
    Carlos Muñoz about 5 years
    @ppumkin tell that to a blind programmer. The text can be read by screen readers though
  • Piotr Kula
    Piotr Kula about 5 years
    Hi Carlos. Yes I understand the situation. It is already tough explaining it people without visibility disabilities. I am not even sure any kind of assistive software would be able to describe what is going on in the picture well to any body. It does bring to attention that the answer should probably have text at least trying to describe what is going on.
  • Erik Frantz
    Erik Frantz over 4 years
    This is what I was running into, confirming my suspicions.
  • eyal
    eyal over 4 years
    Worked for me! thnks!
  • apc
    apc about 4 years
    I had changed the assembly name and had some old dlls sitting in the bin. Thanks
  • Vash
    Vash about 4 years
    Thank you! I cannot believe I missed something so simple.
  • Milad
    Milad almost 4 years
    For me after clean up I had to modify Global.asax code behind
  • ransems
    ransems almost 3 years
    "AppName.Controllers" is actually "MyNamespace.Controllers" for clarity, in case you are looking for the AppName registered somewhere, it's just the namespace of the controller that should be the default route handler.
  • AlejandroDG
    AlejandroDG over 2 years
    Thanks MATE, this was my problem, damn VS clean useless method.
  • suttonb1
    suttonb1 over 2 years
    9 years later, and I just ran into this issue. Made a copy of a project with a new name. Published the new project. Was working fine in my new dev environment, but not when I overwrote the existing staging and prod sites. This was exactly the reason! Thanks so much!