简体   繁体   中英

Multiple types found routing error even though a namespace is specified

related:

Multiple types were found that match the controller named 'Home'

Multiple types were found that match the controller named 'Home' - In two different Areas


Multiple types were found that match the controller named 'FW'.

The request for 'FW' has found the following matching controllers:

app.Controllers.Admin.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

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

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.

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.

can you try and rearrage your rotes like this. The problem might be arising because app.Controllers.Admin lies in 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. 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.

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.

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.

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

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. That could possibly be the reason i believe. Hope it helped you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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