1

I have this message when I run my Web Api code

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 request for 'Home' has found the following matching controllers:
A.Controllers.HomeController
N.Controllers.HomeController

But I have only 1 Home controller. What is happening?

4

2 回答 2

2

You probably renamed your MVC project and the old DLL is still in the bin folder. Remember that ASP.NET MVC loads controllers from all assemblies present in the bin folder. You will have to look for all assemblies in your bin folder and make sure that you don't have the same controller twice.

In fact it is possible to use the same controller name twice in the same application. Let's suppose for example that you wanted to have a HomeController in your main part and a HomeController in some Area. When configuring the routes you could specify a namespace constraint to the namespace that will contain the controllers:

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

and in your Area:

context.MapRoute(
    name: "Admin_default",
    url: "Admin/{controller}/{action}/{id}",
    defaults: new { action = "Index", id = UrlParameter.Optional },
    namespaces: new[] { "MvcApplication1.Areas.Admin.Controllers" }
);
于 2013-03-30T11:36:09.473 回答
1

Look under( Home ) name in edit-> find. and see if it's used in anyother webpages. also make sure the page doesn't inherit this control from any other pages.

于 2013-03-30T11:39:04.957 回答