简体   繁体   English

即使指定了名称空间,也会发现多种类型的路由错误

[英]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.Controllerapp.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.

相关问题 未找到命名空间,即使它在另一个文件中声明 - Namespace not found, even though it is declared in another file 属性路由引发“找到多种控制器类型”错误 - Attribute routing throws a “multiple controller types found” error 类型或名称空间可以找到-即使我声明了变量 - type or namespace could be found - even though i declared the variable 即使统一构建,omnisharp 也找不到命名空间 - namespace can't be found by omnisharp even though unity builds 即使安装了MVC并具有using语句,也出现错误“找不到类型或名称空间名称'SelectList'” - Getting error “The type or namespace name 'SelectList' could not be found” even though I have MVC installed, and have the using statement 即使指定了多个文件上传,IFormFile IEnumerable 的大小也是 1 - IFormFile IEnumerable is of size 1 even though multiple file upload is specified 在命名空间中存在类,但未找到XMLNS名称错误 - Getting XMLNS name not found error though class exist in namespace 即使文件夹存在于 c# 中,也找不到文件夹错误 - Folder not found error even though the folder exists in c# 方法即使在那儿也找不到 - Method Not being found even though it is there 为什么即使我用ID指定一个,也要选择多个CheckBoxList中的值 - Why does the value in multiple CheckBoxList selected even though I specified one with the ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM