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

C#asp.net Mvc-3Controller

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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 2 - C#

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 3 - C#

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 - C#

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 - C#

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

Solution 6 - C#

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!

Solution 7 - C#

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

Solution 8 - C#

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");

Solution 9 - C#

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

Solution 10 - C#

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.

Solution 11 - C#

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) }
        );

Solution 12 - C#

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.

Solution 13 - C#

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
   {

Solution 14 - C#

In Route.config

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

Solution 15 - C#

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.

Solution 16 - C#

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

Solution 17 - C#

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.

Solution 18 - C#

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

Solution 19 - C#

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

Solution 20 - C#

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

Solution 21 - C#

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.

Solution 22 - C#

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.

Solution 23 - C#

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

Solution 24 - C#

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

Solution 25 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOnly Bolivian HereView Question on Stackoverflow
Solution 1 - C#Kirk WollView Answer on Stackoverflow
Solution 2 - C#Darin DimitrovView Answer on Stackoverflow
Solution 3 - C#DeveloperView Answer on Stackoverflow
Solution 4 - C#TomView Answer on Stackoverflow
Solution 5 - C#Sruit A.SukView Answer on Stackoverflow
Solution 6 - C#Dr TJView Answer on Stackoverflow
Solution 7 - C#Amir ShresthaView Answer on Stackoverflow
Solution 8 - C#Ben FosterView Answer on Stackoverflow
Solution 9 - C#StanKView Answer on Stackoverflow
Solution 10 - C#Mech0zView Answer on Stackoverflow
Solution 11 - C#XtremexploitView Answer on Stackoverflow
Solution 12 - C#Allan ElderView Answer on Stackoverflow
Solution 13 - C#T GuptaView Answer on Stackoverflow
Solution 14 - C#AwaisView Answer on Stackoverflow
Solution 15 - C#Arman HayotsView Answer on Stackoverflow
Solution 16 - C#javad hematiView Answer on Stackoverflow
Solution 17 - C#AnthonyJView Answer on Stackoverflow
Solution 18 - C#Sebastian 506563View Answer on Stackoverflow
Solution 19 - C#VivekDevView Answer on Stackoverflow
Solution 20 - C#Umang PatwaView Answer on Stackoverflow
Solution 21 - C#HugoView Answer on Stackoverflow
Solution 22 - C#user1017882View Answer on Stackoverflow
Solution 23 - C#Satish Kumar sonkerView Answer on Stackoverflow
Solution 24 - C#Kashif FarazView Answer on Stackoverflow
Solution 25 - C#Curtis WhiteView Answer on Stackoverflow