[英]Multiple types found routing error even though a namespace is specified
related: 有关:
Multiple types were found that match the controller named 'Home' 找到多个与名为“ Home”的控制器匹配的类型
Multiple types were found that match the controller named 'Home' - In two different Areas 发现了多个与名为“ Home”的控制器匹配的类型-在两个不同的区域中
Multiple types were found that match the controller named 'FW'. 找到了多个与名为“ FW”的控制器匹配的类型。
The request for 'FW' has found the following matching controllers:
对“ FW”的请求已找到以下匹配的控制器:
app.Controllers.Admin.FWController
app.Controllers.Admin.FWController
app.Areas.Manage.Controllers.FWController
app.Areas.Manage.Controllers.FWController
I tried the suggestions from those related links. 我尝试了那些相关链接中的建议。 I attempted to differentiate the controllers by using different namespaces:
我试图通过使用不同的名称空间来区分控制器:
global.asax.cs global.asax.cs
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional },
new string[] { "app.Controllers" }
);
routes.MapRoute(
"Default_Admin_Top", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional },
new string[] { "app.Controllers.Admin" }
);
in the manage area ManageAreaRegistration
在管理区域
ManageAreaRegistration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Manage_default",
"Manage/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "app.Areas.Manage.Controllers" }
);
}
I also looked in the bin
folder for an old version but there was only the current one. 我还在
bin
文件夹中查找了旧版本,但是只有当前版本。
What am I missing? 我想念什么? It seems like this should work.
看来这应该工作。
The issue appears to revolve around the fact that I gave my controller its own namespace without it being in its own area: 问题似乎与以下事实有关:我为控制器提供了自己的名称空间,而没有将其放在自己的区域中:
namespace app.Controllers.Admin
{
public class FWController : Controller{}
}
Removing the .Admin
from the namespace here will remove the collision and also the error, but I do not fully understand why. 从此处的命名空间中删除
.Admin
会删除冲突以及错误,但是我不完全理解为什么。
can you try and rearrage your rotes like this. 您可以尝试这样重装死记硬背吗? The problem might be arising because
app.Controllers.Admin
lies in app.Controller
该问题可能是由于
app.Controllers.Admin
位于app.Controller
中app.Controller
routes.MapRoute(
"Default_Admin_Top", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional },
new string[] { "app.Controllers.Admin" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional },
new string[] { "app.Controllers" }
);
it looks similar to an issue which i have faced lately. 它看起来类似于我最近遇到的一个问题。
according to your scenario and having experienced the same issue, i suspect the problem would be the naming convention with controllers. 根据您的情况并遇到相同的问题,我怀疑问题可能是控制器的命名约定。 Say for instance, your application has a controller named
FWController
in your root structure before re-structuring/maintaining your code in separate areas. 举例来说,您的应用程序在根结构中的根结构中有一个名为
FWController
的控制器,然后在单独的区域中FWController
/维护代码。 when your application starts, the Application_Start()
method inside Global.asax
file registers route to route collection table (one of the core features of MVC 3 pattern) which maintain your incoming request when an action method executes inside a controller. 当您的应用程序启动时,
Global.asax
文件中的Application_Start()
方法将路由注册到路由收集表(MVC 3模式的核心功能之一),该表将在操作方法在控制器内部执行时维护您的传入请求。
problem arises when you have a controller with same name like in your case FWController
and directory structure of area somehow mimics the root directory just as you see in one of the referenced urls. 当你有一个同名的控制器像你的情况出现问题
FWController
和地区的目录结构在某种程度上模仿就像你在引用的一个URL看到的根目录。
I implemented the similar solution as one of the URL justifies, ie 我实现了类似的解决方案,因为其中一个网址是合理的,即
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, //Parameter defaults
new []{"MyProject.WWW.Controllers"}
);
i did not touch my area registration.cs file at all. 我根本没有触摸我的区域registration.cs文件。
When you rename the namespace, your routing starts pointing to the app.Controllers.FWController
(not in app.Areas.Manage.Controllers.FWController
), which resolved the conflict that was occurring before because you specified this in your Global.asax
重命名名称空间时,路由开始指向
app.Controllers.FWController
(不在app.Areas.Manage.Controllers.FWController
),这解决了之前发生的冲突,因为您在Global.asax
指定了此冲突
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional },
new string[] { "app.Controllers" }
);
that request also neglects the MapRoute
that contains .admin
namespace. 该请求还忽略了包含
.admin
名称空间的MapRoute
。 That could possibly be the reason i believe. 这可能是我相信的原因。 Hope it helped you.
希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.