Sunday, 22 February 2015

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

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.


This error message often happens when  you're trying to implement Areas in your project and you added two controller name with same inside 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 you declaring your routes. So in main routes definition in Global.asax file you can use following line of code.


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

and in your AdminAreaRegistration.cs

context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional }, new[] { "App.Web.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.

No comments:

Post a Comment