Multiple Types Were Found That Match the Controller Named 'Home'

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

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.

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

The error is giving away the answer basically you have multiple controllers named HomeController. I would assume you have not deleted the original IdentitySample.Controllers.HomeController.

You have 2 options.

  1. Delete the IdentitySample.Controllers.HomeController instance.
  2. Change your routes so your routes include the namespace to search (as listed in the error).

If you would like to go with option #2 then in your route table change the default route from

routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
);

to

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

in App_Start\RouteConfig.cs

Where in the second example is telling to look for the controllers in the "RecreationalServicesTicketingSystem.Controllers" namespace.

ASP.NET MVC - Multiple types were found that match the controller

I found a method that solved my problem, I had to add the default namespace on the application start event via the "ControllerBuilder.Current.DefaultNamespaces.Add" method:

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.DefaultNamespaces.Add("Stock.Controllers"); // Add This
}

Namespace change - Multiple types were found that match the controller named 'Home'

After doing some research, I found that the cause was the old mvc app dll remaining in the bin folder (bin\oldns.dll). Clean was not clearing it out. I manually deleted the contents of the bin directory and then all was well.

Multiple types were found that match the controller named 'Home' - In two different Areas

I don't know what happen, but this code works fine:

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 },
new string[] { "BaseAdminMVC.Areas.TitomsAdmin.Controllers" }
);
}

Multiple types were found that match the controller named 'Account'. MVC 4 & using RouteConfig.CS

you need to use the version with this signature

public static Route MapRoute(
this RouteCollection routes,
string name,
string url,
Object defaults,
string[] namespaces )

To make it work just set up two almost identical routes - one which includes the project1 for the namespaces parameter and then a second identical version but use project2 in the namespaces parameter. That said, it would generally be less confusing, to use different names if you can...

routes.MapRoute(
name: "Default_Project1",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional,
namespaces: new string[] { "Project1" } }
);

routes.MapRoute(
name: "Default_Project2",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional,
namespaces: new string[] { "Project2" }
);

Multiple types were found that match the controller

I tried following. Works as of now -

public class CustomControllerSelector : DefaultHttpControllerSelector
{
//const string partName = "Webapi.Controllers";
private readonly HttpConfiguration _config;

public CustomControllerSelector(HttpConfiguration config)
: base(config)
{
_config = config;
}

public override System.Web.Http.Controllers.HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
var _route = request.GetRouteData();

var controllerName = base.GetControllerName(request);

var type = _config.Services.GetAssembliesResolver();
var controlles = _config.Services.GetHttpControllerTypeResolver().GetControllerTypes(type);

object name;
_route.Values.TryGetValue("route", out name);

//No more hard coding
var partName = controllers.FirstOrDefault().Namespace;
var st = name as string;
if (st != null)
{
var conType = controlles.FirstOrDefault(a => a.Namespace == string.Format("{0}.{1}", partName, st));
if (conType != null)
return new System.Web.Http.Controllers.HttpControllerDescriptor(_config, controllerName, conType);
}

return base.SelectController(request);
}
}

In the WebApiConfig.cs -

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{route}/{id}",
defaults: new { id = RouteParameter.Optional }
);

Test routes -

http://localhost:60957/api/Another/Route/a
http://localhost:60957/api/Another/Route2/aaaaa


Related Topics



Leave a reply



Submit