Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 570
Only Bolivian Here Avatar asked Oct 20 '11 21:10

Only Bolivian Here


People also ask

Can we have multiple controllers in MVC?

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.

What are controllers used for in MVC?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.

Can two controllers have same view?

Yes, It is possible to share a view across multiple controllers by putting a view into the shared folder. By doing like this, you can automatically make the view available across multiple controllers.


2 Answers

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)

like image 176
Kirk Woll Avatar answered Oct 12 '22 21:10

Kirk Woll


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.

like image 39
Darin Dimitrov Avatar answered Oct 12 '22 20:10

Darin Dimitrov