ASP.NET MVC 2 中具有一个名称的多个控制器
尝试运行 ASP.NET MVC 应用程序时收到以下错误:
“帐户”请求已找到以下匹配的控制器:
uqs.Controllers.Admin.AccountController
MvcApplication1.Controllers.AccountController
我在项目中搜索了 MvcApplication1.Controllers.AccountController
以将其删除,但找不到匹配项。
我尝试注册一条路线来修复它:
routes.MapRoute(
"LogAccount", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = "" },
new string[] { "uqs.Controllers.Admin" } // Parameter defaults
);
但这并没有解决问题。
发现多种类型匹配 名为“Account”的控制器。
我该如何解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您重构项目并将默认命名空间和程序集从“MVCApplication1”更改为“uqs”,则您的 bin 目录中最终可能会出现 2 个程序集(新的和旧的)。您可能会收到此错误,因为 AccountController 位于两个程序集中。
清理 bin 目录中的旧 MVCApplication1.dll。
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.
即使在不同的命名空间中,您的应用程序中也不能有多个名为
Account
的控制器。您必须按
Area
(ASP.NET MVC 2 中的一项功能)划分这些控制器。如果您对
AccountController
进行查找,您将在应用程序中找到所有名为Account
的控制器;如果您想要同时使用它们,请将它们移至不同的区域
,或者删除一个。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 namedAccount
in your application; and move them off into differentAreas
if you want both of them, or delete one.有同样的问题。清理了垃圾箱,我就可以走了。
Had this same problem. Cleaned the bin and I was good to go.
即使提供了命名空间,问题上也会出现稍微令人困惑的变化(类似地,它会导致相同的错误消息)。我认为 MVC 3 在这方面比 MVC 2 更挑剔一些。
简短回答:
确保控制器的命名空间实际上是
MapRoute
调用中指定的命名空间!长答案:
我有 3 个区域:
default ("")
/Facebook
/Store
并且它们每个都有 < code>AdminController我有这样映射的路由(对于我的默认区域):
对 /admin 的请求给出了以下错误:
但是等一下!我没有指定控制器命名空间吗...?这是怎么回事.... ?
事实证明,我的默认区域的管理控制器命名空间是
RR_MVC.Controller
而不是Rolling_Razor_MVC.Controller.Main
。由于某种原因,在 MVC 2 中这并没有出现问题,但在 MVC 3 中却出现了问题。我认为 MVC 3 只是要求您在存在潜在歧义时更加明确。
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 haveAdminController
I have the route mapped like this (for my default area):
A request to /admin gave the following error :
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 ofRolling_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
由 ASP.NET MVC Visual Studio 模板自动生成。它位于Controllers\AccountController.cs
中。尝试在项目中搜索并删除它。AccountController
is automatically generated by the ASP.NET MVC Visual Studio template. It is located inControllers\AccountController.cs
. Try searching for it in the project and delete it.我遇到了这个问题...
通过删除 .csproj 文件之一中的项目引用来解决
I had this problem...
Solved by removing a project reference in one of the .csproj files