5

I tried implementing Phil's Areas Demo in my project

http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx.

I appended the Areas/Blog structure in my existing MVC project and I get 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 how my Global.asax looks.

public static void RegisterRoutes(RouteCollection routes)
{
    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 the /Areas/Blogs from routes.MapAreas, it looks at the Index of the root.

3 Answers 3

3

In ASP.NET MVC 2.0, you can include the namespace(s) 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 restricts the route to searching for controllers only in the namespace you specified.

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

I'm not sure I like that solution. The way the projects are desinged, the main controllers/views folder are clearly their own "Area", they just don't have an assigned name. I'd like for that to continue to function as is.
You can do this if you configure your route to use "main" as the default area.
0

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

E.g., I have one controller named HomeController in Areas/Admin/HomeController and another in root /controller/HomeController
so I prioritize my root one 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.
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.