views:

195

answers:

4

Hey,

Has anyone been able to get the Areas in ASP.NET MVC 2 to work?

I created a new Area called "Secure" and placed a new controller in it named HomeController. I then Created a new Home/Index.aspx view. However, when I browse to http://localhost/myapp/Secure/ it gives a 404 resource cannot be found. http://localhost/myapp/Secure/Home gives the same error.

My area registration looks like this:

public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Secure_default",
                "Secure/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }

I also tried this:

public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Secure_default",
                "Secure/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

Thanks, Justin

A: 

Are you calling AreaRegistration.RegisterAllAreas() in your global.asax?

Sub Application_Start()
    AreaRegistration.RegisterAllAreas()

    RegisterRoutes(RouteTable.Routes)
End Sub
Tommy
Pretty sure that's put there by default when creating a new MVC2 Web Application. Unless he has manually removed it by mistake of course.
RPM1984
Yes that's in there.
Justin
A: 

Sure, i've got Areas working with ASP.NET MVC 2.

Its hard for people to debug routes over SO, the easiest way is for you to use Phil Haacks Route Debugger. It'll tell you what routes (and areas) are being resolved for a particular URL. Extremely handy.

However ill take a stab, try changing your route to this:

       context.MapRoute(
            "Secure_default",
            "Secure",
            new { action = "Index", controller = "Home", id = UrlParameter.Optional }
        );

The url (<yourhost>/Secure) will find your area Secure but not know which controller you hand the request to, as you have not specified a default value for controller in your areas route.

Here's my setup:

Areas
      Albums
           Controllers
                AlbumsController.cs (namespace Web.Areas.Albums.Controllers)
           Models
           Views
                Albums
                     Index.aspxx
           AlbumsAreaRegistration.cs
                context.MapRoute(
                    "Albums_Default",
                    "Albums",
                    new { controller = "Albums", action = "Index", id = UrlParameter.Optional)

The URL: http://localhost/Albums triggers the "Index" action of my "AlbumsController" in my "Albums" area.

What does you structure look like?

RPM1984
Changing the MapRoute to that didn't have any effect. I added his route debugger DLL and when I go to /secure it says it matches even though without the debugger DLL I get a 404:Matched Route: Secure/{controller}/{action}/{id}True Secure/{controller}/{action}/{id} controller = Home, action = Index, id = System.Web.Mvc.UrlParameter (null) Namespaces = System.String[], area = Secure, UseNamespaceFallback = False
Justin
Could namespaces be an issue? My SecureAreaRegistration class is in namespace TothSolutions.Web.Areas.Secure and my HomeController class is in namespace TothSolutions.Web.Areas.Secure.Controllers.
Justin
No - that namespace is correct. I'll update my answer with my structure for my Area.
RPM1984
Mine looks the same except Secure instead of Albums. :( The worst part is that the route tester says "True" for "Matches Current Request" for the Url "Secure" when I browse to http://localhost/TothSolutions/secure, very sad...
Justin
Also I should point out that I've tried using the Visual Studio server instead of IIS - http://localhost:99/Secure - and that gives the same 404...
Justin
Interestingly, with the same exact code it works for another developer so it has to be configuration related. I've tried the Visual Studio server, an IIS subdirectory, and the IIS root and all give the 404, anything else I should try?
Justin
Hmm....thats interesting. So both you and the other developer have the same code, both use VS Server, theirs works and yours dont? Thats bizarre. Maybe temp files hanging around? (try a clean solution).Sorry mate - ive run out of ideas. =)
RPM1984
Yeah I tried clean solution, didn't work. Thanks for the help, I'm at a loss as well... I'm going to try it on my 2nd computer and see if that works.
Justin
A: 

Have you tried using http://localhost/myapp/Secure/Home/index ? I find so many times that when I use index as the view name and don't specify it in the path it never works. It should work but it never works for me.

I don't like calling my views index anyways so not a big deal for me.

chobo2
Yes I've tried that and it still gives a 404.
Justin
+1  A: 

I got it working, the answer was to change the area registration class to this:

context.MapRoute(
                "Secure_Default", // Route name
                "Secure/{controller}/{action}/{id}", // URL with parameters
                new { area="Secure", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new[] { typeof(Controllers.HomeController).Namespace }
            );

I have no idea why it worked for another developer and not for me, also I feel like it should "just work" out of the box when you create a new Area, you shouldn't have to fiddle with the route mapping.

Anyway, thanks all for the help and I'm giving the answer to RPM for all his hard work.

Justin