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

发布于 2024-08-28 19:47:44 字数 697 浏览 6 评论 0原文

尝试运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

究竟谁懂我的在乎 2024-09-04 19:47:44

如果您重构项目并将默认命名空间和程序集从“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.

真心难拥有 2024-09-04 19:47:44

即使在不同的命名空间中,您的应用程序中也不能有多个名为 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 named Account in your application; and move them off into different Areas if you want both of them, or delete one.

国粹 2024-09-04 19:47:44

有同样的问题。清理了垃圾箱,我就可以走了。

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

假装爱人 2024-09-04 19:47:44

即使提供了命名空间,问题上也会出现稍微令人困惑的变化(类似地,它会导致相同的错误消息)。我认为 MVC 3 在这方面比 MVC 2 更挑剔一些。


简短回答:

确保控制器的命名空间实际上是 MapRoute 调用中指定的命名空间!


长答案

我有 3 个区域:default ("") / Facebook / Store 并且它们每个都有 < code>AdminController

我有这样映射的路由(对于我的默认区域):

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

对 /admin 的请求给出了以下错误:

发现多种类型匹配
名为“admin”的控制器。这个可以
如果服务于此的路线会发生
请求('{控制器}/{动作}/{id}')
未指定名称空间...

“admin”的请求已找到
以下匹配控制器:

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

但是等一下!我没有指定控制器命名空间吗...?这是怎么回事.... ?

事实证明,我的默认区域的管理控制器命名空间是 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 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.

霊感 2024-09-04 19:47:44

AccountController 由 ASP.NET MVC Visual Studio 模板自动生成。它位于 Controllers\AccountController.cs 中。尝试在项目中搜索并删除它。

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.

忆伤 2024-09-04 19:47:44

我遇到了这个问题...

通过删除 .csproj 文件之一中的项目引用来解决

I had this problem...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文