MVC3 Url Routing to create member area

Go To StackoverFlow.com

2

I would like to create a member area on my site with the following URL patterns:

Pattern for logged out user:

domain.com
domain.com/About
domain.com/Blog
domain.com/Blog/1 (where 1 is the post ID)

But I also have a member area where I prefix the Url with Member like this:

domain.com/Member/MyProfile
domain.com/Member/MySettings

This seems simple, but I can't see an obvious way to make routing rules for this. I have:

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

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

This works great for the member when you are logged in, but unfortunately the first rule also matches the logged out view and Url.Action("Blog", "Home") produces a Url that looks like this:

domain.com/Member/Home/Blog

How do I tell Url.Action that it must form Urls with the default rule outside the member area?

2012-04-04 17:34
by AldenG
I don't understand the issue. With the default route, all you have to do is create a MemberController with Action Methods of MyProfile and MySetting - Erik Funkenbusch 2012-04-04 17:42


1

You could use a real MVC area instead of trying to simulate one. There's also a video you might checkout. The idea is that you leave your default route definition in Global.asax and then add a Member area to your site which will have a separate route configuration.

2012-04-04 17:38
by Darin Dimitrov
This definitely looks like the way to go, and I created the member area, moved my controllers and views, fixed up the namespace and the URLs look right and the contoller is being called in its new home. But MVC is not finding the views there. It is still only searching the root Views folder. How do I tell it to look for area-specific views for the area-specific controllers - AldenG 2012-04-04 18:36
+100 for the video link. Sum my hours of digging up to 3 minutes - Tom 2012-07-06 08:34
Ads