Multiple types were found that match the controller named 'Home'
Asked Answered
R

25

343

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?

Rudolfrudolfo answered 20/10, 2011 at 21:9 Comment(2)
Very unclear question. Are you using areas? Does the problem occur locally?Revitalize
@Darin: Edited that information in.Rudolfrudolfo
R
499

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.

Revitalize answered 20/10, 2011 at 21:17 Comment(9)
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?Rudolfrudolfo
@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.Revitalize
Thanks. In ASP MVC 4.0 you need to pass named argument like namespaces: new[] {"AppName.Areas.Admin.Controllers" }Cumulous
+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 :)Hooray
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.Mendelevium
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?Karl
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.Futuristic
i was facing the similar issue. and main reason was that i had the same controller in two different AreaAdelia
"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.Gallon
E
557

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)

Efrem answered 28/9, 2012 at 4:22 Comment(9)
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.Unconcern
If you are getting this from an Azure App Service, go to https://<your_app_name_here>.scm.azurewebsites.net/DebugConsole to log in and delete files.Venu
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 cleanParfitt
I got this when moving my project files to a second drive. Clearing out the bin folder solves it. Oddest darn thing.Extremism
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.Rapport
@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.Trilemma
Seems Clean and Rebuild don't clear bin folder. Manually we have to delete.Veradia
For me after clean up I had to modify Global.asax code behindArgal
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!Apropos
R
499

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.

Revitalize answered 20/10, 2011 at 21:17 Comment(9)
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?Rudolfrudolfo
@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.Revitalize
Thanks. In ASP MVC 4.0 you need to pass named argument like namespaces: new[] {"AppName.Areas.Admin.Controllers" }Cumulous
+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 :)Hooray
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.Mendelevium
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?Karl
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.Futuristic
i was facing the similar issue. and main reason was that i had the same controller in two different AreaAdelia
"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.Gallon
T
67

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" }
            );
Trabzon answered 20/10, 2014 at 11:0 Comment(0)
D
38

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

Deputy answered 6/7, 2012 at 11:13 Comment(3)
Solved the issue..! :)Kelle
@ppumkin tell that to a blind programmer. The text can be read by screen readers thoughDennard
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.Higbee
H
34

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

Hydrocephalus answered 20/9, 2017 at 3:1 Comment(4)
Exactly my issue. Thank you.Xl
Worked for me! thnks!Titanium
I had changed the assembly name and had some old dlls sitting in the bin. ThanksSalinas
Thank you! I cannot believe I missed something so simple.Dinosaurian
F
32

What others said is correct but for those who still face the same problem:
In my case it happened because I copied another project n renamed it to something else BUT previous output files in bin folder were still there... And unfortunately, hitting Build -> Clean Solution after renaming the project and its Namespaces doesn't remove them... so deleting them manually solved my problem!

Formula answered 30/11, 2014 at 5:24 Comment(3)
your suggestion saved meElbert
me too, thanks , clean dosnt actually mean clean , grrrrLexicologist
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!Dawes
S
24

Check the bin folder if there is another dll file that may have conflict the homeController class.

Scopophilia answered 20/7, 2014 at 5:56 Comment(2)
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!Oddball
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.Cableway
Z
13

Another solution is to register a default namespace with ControllerBuilder. Since we had lots of routes in our main application and only a single generic route in our areas (where we were already specifying a namespace), we found this to be the easiest solution:

ControllerBuilder.Current
     .DefaultNamespaces.Add("YourApp.Controllers");
Ziguard answered 10/5, 2012 at 21:28 Comment(2)
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.Occupation
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.Kernel
L
7

Even though you are not using areas, you can still specify in your RouteMap which namespace to use

routes.MapRoute(
    "Default",
    "{controller}/{action}",
    new { controller = "Home", action = "Index" },
    new[] { "NameSpace.OfYour.Controllers" }
);

But it sounds like the actual issue is the way your two apps are set up in IIS

Lohrman answered 20/10, 2011 at 21:35 Comment(0)
H
6

I just had this issue, but only when I published to my website, on my local debug it ran fine. I found I had to use the FTP from my webhost and go into my publish dir and delete the files in the BIN folder, deleting them locally did nothing when I published.

Hookup answered 30/11, 2011 at 22:9 Comment(2)
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.Hintz
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.Sluff
S
6

if you want to resolve it automatically.. you can use the application assambly just add the following code:

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { string.Format("{0}.Controllers", BuildManager.GetGlobalAsaxType().BaseType.Assembly.GetName().Name) }
        );
Specialty answered 28/5, 2015 at 15:57 Comment(1)
great solution if you have same controllers in multiple projectsInland
V
5

You can also get the 500 error if you add your own assembly that contains the ApiController by overriding GetAssemblies of the DefaultAssembliesResolver and it is already in the array from base.GetAssemblies()

Case in point:

public class MyAssembliesResolver : DefaultAssembliesResolver
{
    public override ICollection<Assembly> GetAssemblies()
    {
        var baseAssemblies = base.GetAssemblies();

        var assemblies = new List<Assembly>(baseAssemblies);

        assemblies.Add(Assembly.GetAssembly(typeof(MyAssembliesResolver)));

        return new List<Assembly>(assemblies);
    }
}

if the above code is in the same assembly as your Controller, that assembly will be in the list twice and will generate a 500 error since the Web API doesn't know which one to use.

Vonnie answered 4/4, 2013 at 17:0 Comment(0)
P
5

There might be another case with Areas even you have followed all steps in routing in Areas(like giving Namespaces in global routing table), which is:

You might not have wrapped your Global Controller(s) in 'namespace' you provided in routing.

Eg:

Done this:

public class HomeController : Controller
{

Instead of:

namespace GivenNamespace.Controllers
{
   public class HomeController : Controller
   {
Puke answered 24/9, 2013 at 10:52 Comment(1)
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!Gina
H
4

In Route.config

namespaces: new[] { "Appname.Controllers" }

Hollah answered 25/4, 2016 at 8:23 Comment(0)
T
3

i just deleted folder 'Bin' from server and copy my bin to server, and my problem solved.

Threaten answered 27/2, 2016 at 7:10 Comment(0)
S
3

Got same trouble and nothing helped. The problem is that I actually haven't any duplicates, this error appears after switching project namespace from MyCuteProject to MyCuteProject.Web.

In the end I realized that source of error is a global.asax file — XML markup, not .cs-codebehind. Check namespace in it — that's helped me.

Shylashylock answered 17/3, 2016 at 16:44 Comment(0)
T
3

Some time in a single application this Issue also come In that case select these checkbox when you publish your application enter image description here

Twentyone answered 12/1, 2017 at 12:48 Comment(0)
H
2

Right click the project and select clean the project. Or else completely empty the bin directory and then re-build again. This should clear of any left over assemblies from previous builds

Housing answered 28/11, 2016 at 3:38 Comment(0)
P
2

If it could help other, I've also face this error. The problem was cause by on incorrect reference in me web site. For unknown reason my web site was referring another web site, in the same solution. And once I remove that bad reference, thing began to work properly.

Potash answered 27/4, 2017 at 19:12 Comment(0)
A
1

We found that we got this error when there was a conflict in our build that showed up as a warning.

We did not get the detail until we increased the Visual Studio -> Tools -> Options -> Projects and Solutions -> Build and Run -> MSBuild project build output verbosity to Detailed.

Our project is a .net v4 web application and there was a conflict was between System.Net.Http (v2.0.0.0) and System.Net.Http (v4.0.0.0). Our project referenced the v2 version of the file from a package (included using nuget). When we removed the reference and added a reference to the v4 version then the build worked (without warnings) and the error was fixed.

Acromion answered 17/6, 2014 at 23:0 Comment(0)
U
1

Other variation of this error is when you use resharper and you use some "auto" refactor options that include namespace name changing. This is what happen to me. To solve issue with this kind of scenario delete folderbin

Unconcern answered 20/2, 2016 at 23:6 Comment(1)
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 folderKala
L
0

If you're working in Episerver, or another MVC-based CMS, you may find that that particular controller name has already been claimed.

This happened to me when attempting to create a controller called FileUpload.

Latona answered 25/5, 2017 at 11:11 Comment(0)
A
0

i was facing the similar issue. and main reason was that i had the same controller in two different Area. once i remove the one of them its working fine.

i have it will helpful for you.

Project Solution

Adelia answered 13/9, 2017 at 4:56 Comment(0)
R
0

I have two Project in one Solution with Same Controller Name. I Removed second Project Reference in first Project and Issue is Resolved

Revisionist answered 13/11, 2018 at 3:37 Comment(0)
S
0

I have found this error can occur with traditional ASP.NET website when you create the Controller in non App_Code directory (sometimes Visual Studio prevents this).

It sets the file type to "Compile" whereas any code added to "App_Code" is set to "Content". If you copy or move the file into App_Code then it is still set as "Compile".

I suspect it has something to with Website Project operation as website projects do not have any build operation.Clearing the bin folder and changing to "Content" seems to fix it.

Shebat answered 4/4, 2019 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.