Tags: | Posted by AlexanderZeitler on 10/26/2010 9:05 PM | Comments (0)

Wenn man in ASP.NET MVC (auch über Areas hinweg) mehrere Controller mit dem gleichen Namen verwendet, erhält man zunächst folgende Fehlermeldung:

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.

Der letzte Satz beinhaltet hierbei bereits die Lösung des Problems, welche allerdings gerne übersehen wird:

Anstelle der klassischen Registrierung des Default-Controllers wie hier:

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

verwendet man einfach die entsprechende Überladung von MapRoute, sodass explizit der Namespace angegeben wird, in dem sich der Default-Controller befindet, z.B. "MyMvcApplication.Controllers":

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

Verwendet man AreaReigstrations, müssen auch dort die Default-Controller-Definition angepasst werden.

DotNetKicks-DE Image

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Comments are closed