tags:

views:

242

answers:

2

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.

+1  A: 

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.

Craig Stuntz
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.
midas06
You can do this if you configure your route to use "main" as the default area.
Craig Stuntz
A: 

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.

Robert Harvey