Ambiguous controller names in ASP.NET MVC

I tried implementing a Phil Areas Demo demo in my project http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx . I added the Areas / Blog structure in my existing MVC project and I got the following error in my project.

The controller name "Home" is ambiguous between the following types: WebMVC.Controllers.HomeController WebMVC.Areas.Blogs.Controllers.HomeController

this is what my global.asax looks like.

public static void RegisterRoutes (routes RouteCollection) {routes.IgnoreRoute ("{resource} .axd / {* PathInfo}");

routes.MapAreas("{controller}/{action}/{id}", "WebMVC.Areas.Blogs", new[] { "Blogs", "Forums" }); routes.MapRootArea("{controller}/{action}/{id}", "WebMVC", new { controller = "Home", action = "Index", id = "" }); //routes.MapRoute( // "Default", // Route name // "{controller}/{action}/{id}",// URL with parameters // new { controller = "Home", action = "Index", id = "" } // // Parameter defaults //); } protected void Application_Start() { String assemblyName = Assembly.GetExecutingAssembly().CodeBase; String path = new Uri(assemblyName).LocalPath; Directory.SetCurrentDirectory(Path.GetDirectoryName(path)); ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new AreaViewEngine()); RegisterRoutes(RouteTable.Routes); // RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes); } 

If I remove / Areas / Blogs from route.MapAreas, it looks at the root index.

+4
source share
3 answers

In ASP.NET MVC 2.0, you can include a namespace for your parent project controllers when registering routes in the parent area.

 routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" }, new string[] { "MyProjectName.Controllers" } ); 

This limits the route to looking for controllers only in the specified namespace.

+2
source

Instead of WebMVC.Areas.Blogs and WebMVC, use WebMVC.Areas.Blogs and WebMVC.Areas.OtherAreaName. Think of the name of the realm as the root of the namespace, not the absolute namespace.

+1
source

You can prioritize between multiple controllers of the same name in routing as follows

For example, I have one controller named HomeController in Areas/Admin/HomeController and another in the root /controller/HomeController
so I prioritize my root as follows:

 routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", // Parameter defaults id = UrlParameter.Optional }, new [] { "MyAppName.Controllers" } // Prioritized namespace which tells the current asp.net mvc pipeline to route for root controller not in areas. ); 
0
source

All Articles