Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having issue with multiple controllers of the same name in my project

I am running into the following error with my ASP.NET MVC 3 project:

Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('Home/{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: MyCompany.MyProject.WebMvc.Controllers.HomeController MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController

I have a HomeController in my default controller folder, with a class name of MyCompany.MyProject.WebMvc.Controllers.HomeController.

My RegisterRoutes method, in my global.asax, looks like:

    public static void RegisterRoutes(RouteCollection routes)     {         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");          routes.MapRoute(             "Default", // Route name             "{controller}/{action}/{id}", // URL with parameters             new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults         );     } 

I then have an area called Company, with a HomeController in the default controller folder for the area, with a class name of MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController.

The RegisterArea method in the CompanyAreaRegistration file looks like:

   public override void RegisterArea(AreaRegistrationContext context)     {         context.MapRoute(             "Company_default",             "Company/{controller}/{action}/{id}",             new { area = "Company", action = "Index", id = UrlParameter.Optional }         );     } 

This is all leading the error I highlighted at the beginning of this post. I am struggling trying to piece together a solution from various other posts, with NO LUCK.

Is it possible to have a HomeController in the default controllers folder and then one in EACH area? If so, do I need to make (assuming I do) changes to my configuration file to make this work?

Any help would be much appreciated!

like image 488
mattruma Avatar asked Feb 23 '11 15:02

mattruma


People also ask

Can a system have more than one controller?

Typically, such systems consist of multiple controllers and expose themselves as a single logical entity. In a majority of the real-time deployments, the controller plane is realized as a cluster of SDN controllers. Multiple controller nodes are to be deployed in different cluster configurations for high availability.

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.


1 Answers

The error message contains the recommended solution: "If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter."

routes.MapRoute(      "Default", // Route name      "{controller}/{action}/{id}", // URL with parameters      new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults      new string[] { "MyCompany.MyProject.WebMvc.Controllers"} ); 

This will make http://server/ go to your HomeController's Index action which is, I think, what you want. http://server/company/home will go to the Company area's HomeController's Index action, as defined in the area registration.

like image 151
David Ruttka Avatar answered Oct 01 '22 15:10

David Ruttka