views:

1060

answers:

2

Hello everyone,

I'm trying to get ASP.NET MVC 2 single-project area registration to work. Tried with Preview 2 and now with Beta version with no luck. I used the "Add area" dialog to create a "NewsModule" area. Created a NewsModuleController inside it and an Index view for it.

The route registration for this Area looks like this:

 context.MapRoute(
                "NewsModule_default",
                "NewsModule/{action}/{id}",
                new { action = "Index", id = "", controller = "NewsModule", area = "NewsModule" }
            );

I added the AreaRegistration.RegisterAllAreas(); call to my Global.asax. Accessing http://localhost/mymvcproj/NewsModule gets a HTTP 404 error.

Using Phil Haack's route debugger, I could confirm that the route is being correctly mapped and catched by this URL, however, it seems like the framework is not being able to locate the Area files, maybe?

Anyone can help?

Thanks, Felipe

A: 

Try removing "area = "NewsModule" from the route registration in the Area/NewsModule folder. So it appears like,

context.MapRoute(
                "NewsModule_default",
                "NewsModule/{action}/{id}",
                new { action = "Index", id = "", controller = "NewsModule"}
            );

Here is my Account Area Routes Registration,

Location /Areas/Account/

public class AccountAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get { return "Account"; }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Account_default",
                "Account/{controller}/{action}/{id}",
                new { controller = "", action = "", id = "" }
            );
        }
    }
Pino
Originally, I didn't have the area argument there, I tried adding it to see if that solved the problem, but did not. The result is the same with or without specifying the area.
Felipe Lima
Are you sure your using the Version 2 DLL?
Pino
Does the controller action exist?
Pino
Pino, I downloaded the MVC 2 Beta installer. I had the Preview 2 installed previously, which I uninstalled prior to installing the Beta. Yes, the controller action exists.
Felipe Lima
A: 

Problem solved. Here is what I did to solve:

AreaRegistration.cs file:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "NewsModule_default",
        "NewsModule/{controller}/{action}/{id}",
        new { controller = "NewsModule", action = "Index", id = "" });
}

IMPORTANT: Add the ".Areas." to the namespace (Namespace.Areas.ControllerName).

Global.asax.cs:

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

    // must be called RIGHT AFTER IgnoreRoute()
    AreaRegistration.RegisterAllAreas();

    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );

    //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}
Felipe Lima
If you ever add unit tests to your MVC application that call RegisterRoutes, they will throw some weird `CompilationLock` exceptions with that order of calls; it has to do with calling the static `RegisterAllAreas` in the non-static `RegisterRoutes`. The solution to that issue is described in [another question](http://stackoverflow.com/questions/2851514/compliationlock-throws-httpexception-when-registering-areas-for-asp-net-mvc-unit/2851518#2851518). Doing that solution, though, if you want to test routing for a particular area, you have to explicitly register the area's routes in the test.
patridge