Search code examples
asp.net-mvc-3routesnamespacesareas

MVC3 Routing areas to root using namespace


I have a project with two areas. The two areas allow for different member types who don't share any controllers or views (eg student and teacher). However they both share the root controllers for Contact and Support pages etc.

Currently I use the namespace to route within each area.

I want to do something like this but give priority to the namespaces so they don't have conflicting controllers:

context.MapRoute(
    "Student_Default",
    "{controller}/{action}/{id}",
    new { area = "Student", controller = "Home", action = "Index", id = UrlParameter.Optional },
    new { RoleConstraint = new AuthenticationConstraint() },
    new[] { "Test.Web.Areas.Student.Controllers", "Test.Web.Controllers" }
);

Alternatively I tried using "UseNamespaceFallback" but causes a search in all areas which I don't want.

Something else I have not tried is only registering the area when the user logs on. Would this be an acceptable approach?

Also I could just map each page but there will potentially be a lot and it would be messy to do each one.

So the question is how can I make the root controllers available to both areas without the areas being available to each other? Let me know if more information is needed, I'd be happy to give more about what I'm trying to do.


Solution

  • After thinking about this though the weekend I decided to use "UseNamespaceFallback". I also implemented user roles to manage which areas each user has access to. I think this is a better approach as I had read a recommendation to never use namespace as a method for authentication.