简体   繁体   English

ASP.NET MVC 2中具有一个名称的多个控制器

[英]Multiple Controllers with one Name in ASP.NET MVC 2

I receive the following error when trying to run my ASP.NET MVC application: 尝试运行我的ASP.NET MVC应用程序时收到以下错误:

The request for 'Account' has found the following matching controllers: uqs.Controllers.Admin.AccountController MvcApplication1.Controllers.AccountController 对'Account'的请求找到了以下匹配的控制器: 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. 我在项目中搜索了MvcApplication1.Controllers.AccountController来删除它,但我找不到匹配项。

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). 如果您重构项目并将默认命名空间和程序集从“MVCApplication1”更改为“uqs”,则最终可能会在bin目录(新目录和旧目录)中使用2个程序集。 You can get this error because the AccountController is in both assemblies. 您可以收到此错误,因为AccountController在两个程序集中。

Clean your bin directory of the old MVCApplication1.dll. 清理旧MVCApplication1.dll的bin目录。

You can't have more than one controller named Account in your application, even in different namespaces. 您的应用程序中不能有多个名为Account控制器,即使在不同的命名空间中也是如此。

You have to have these controllers split up by Area (a feature in ASP.NET MVC 2). 您必须按Area (ASP.NET MVC 2中的一项功能)拆分这些控制器。

If you conduct a Find for AccountController you'll find all controllers named Account in your application; 如果您执行Find for AccountController您将在应用程序中找到名为Account所有控制器; and move them off into different Areas if you want both of them, or delete one. 如果你想要它们,或者删除它们,将它们移到不同的Areas

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. 我认为MVC 3在这方面比MVC 2更有吸引力。


Short Answer: 简答:

Make sure the namespace of your controller is in fact the namespace specified in the MapRoute call!! 确保控制器的命名空间实际上是MapRoute调用中指定的命名空间!!


Long Answer : 答案很长

I have 3 areas : default ("") / Facebook / Store and they each have AdminController 我有3个区域: default ("") / Facebook / Store ,每个区域都有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 : 对/ admin的请求发出以下错误:

Multiple types were found that match the controller named 'admin'. 找到了多个与名为“admin”的控制器匹配的类型。 This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces... 如果为此请求提供服务的路由('{controller} / {action} / {id}')未指定名称空间,则会发生这种情况......

The request for 'admin' has found the following matching controllers: 对'admin'的请求找到了以下匹配的控制器:

RR.FacebookControllers.AdminController RR.FacebookControllers.AdminController
RR.Controllers.AdminController RR.Controllers.AdminController
RR.StoreControllers.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 . 事实证明我的默认区域的管理员控制器名称空间是RR_MVC.Controller而不是Rolling_Razor_MVC.Controller.Main

For some reason in MVC 2 this didn't give a problem, but in MVC 3 it does. 出于某种原因,在MVC 2中这没有出现问题,但在MVC 3中确实如此。 I think MVC 3 just requires you to be more explicit when there's potential ambiguities. 我认为MVC 3只是要求你在存在潜在的歧义时更加明确。

AccountController is automatically generated by the ASP.NET MVC Visual Studio template. AccountController由ASP.NET MVC Visual Studio模板自动生成。 It is located in Controllers\\AccountController.cs . 它位于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 通过删除其中一个.csproj文件中的项目引用来解决此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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