简体   繁体   中英

Multiple Controllers with one Name in ASP.NET MVC 2

I receive the following error when trying to run my ASP.NET MVC application:

The request for 'Account' has found the following matching controllers: uqs.Controllers.Admin.AccountController MvcApplication1.Controllers.AccountController

I searched the project for MvcApplication1.Controllers.AccountController to remove it, but I can't find a match.

I try to registered a route to fix it:

 routes.MapRoute(
     "LogAccount", // Route name
     "{controller}/{action}/{id}", // URL with parameters
     new { controller = "Account", action = "LogOn", id = "" },
     new string[] { "uqs.Controllers.Admin" } // Parameter defaults
 );

but that didn't solve it.

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

How I can fix this problem?

If you refactor your project and change the default Namespace and Assembly from "MVCApplication1" to "uqs", you may end up with 2 assemblies in your bin directory (the new one and the old one). You can get this error because the AccountController is in both assemblies.

Clean your bin directory of the old MVCApplication1.dll.

You can't have more than one controller named Account in your application, even in different namespaces.

You have to have these controllers split up by Area (a feature in ASP.NET MVC 2).

If you conduct a Find for AccountController you'll find all controllers named Account in your application; and move them off into different Areas if you want both of them, or delete one.

Had this same problem. Cleaned the bin and I was good to go.

A slightly confusing variation on the problem (similar in that it causes the same error message) can occur even with namespaces supplied. MVC 3 I think is a little pickier than MVC 2 on this front.


Short Answer:

Make sure the namespace of your controller is in fact the namespace specified in the MapRoute call!!


Long Answer :

I have 3 areas : default ("") / Facebook / Store and they each have AdminController

I have the route mapped like this (for my default area):

routes.MapRoute(
     "Default",
     "{controller}/{action}/{id}",
     new { controller = "Gateway", action = "Index", id = UrlParameter.Optional },
     new string[] { "RR.Controllers.Main" }
);

A request to /admin gave the following error :

Multiple types were found that match the controller named 'admin'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces...

The request for 'admin' has found the following matching controllers:

RR.FacebookControllers.AdminController
RR.Controllers.AdminController
RR.StoreControllers.AdminController

But wait a minute! Didn't I specify the controller namespace.... ? What's going on.... ?

Well it turned out my default area's admin controller namespace was RR_MVC.Controller instead of Rolling_Razor_MVC.Controller.Main .

For some reason in MVC 2 this didn't give a problem, but in MVC 3 it does. I think MVC 3 just requires you to be more explicit when there's potential ambiguities.

AccountController is automatically generated by the ASP.NET MVC Visual Studio template. It is located in Controllers\\AccountController.cs . Try searching for it in the project and delete it.

I had this problem...

Solved by removing a project reference in one of the .csproj files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM