newguy
newguy

Reputation: 101

Are nested Areas possible in ASP.NET MVC 2?

I would like to create a project structure with nested areas. For instance I have a "Home" area and underneath this I would like the "News" area that handles it's own route registration and will properly find views when a route points to a controller in the "News" area. By changing the "News" area name to be "Home/News" instead of simply "News", the proper views are found. In the main Global.asax.cs, I instantiate all of the areas ("Home" and "News") and register them individually because the RegisterAllAreas() function only finds areas one level deep (i.e. it only finds and registers the "Home" area).

Has anyone else tried something similar? Is this a major hack or can this be a stable long-term solution? Any advice you can offer is much appreciated.

Upvotes: 8

Views: 783

Answers (2)

Max Toro
Max Toro

Reputation: 28608

You can do it with MvcCodeRouting, a seprate open-source CodePlex project.

MvcCodeRouting automatically creates the best possible routes for your ASP.NET MVC application.

  1. Organize your controllers using namespaces (no more areas) that can go as deep as you want.
  2. Default constraints for primivite types that can be overriden on a per-parameter or per-site basis.
  3. Intelligent grouping of similar routes for efficient matching.
  4. Support for a root controller.
  5. Detection of ambiguous routes.
  6. Formatting of routes (e.g. make them lower case).
  7. Render your routes as calls to the MapRoute extension method, for debugging.
  8. Use the same namespace-based base route for organizing your Views.

Upvotes: 4

Linkgoron
Linkgoron

Reputation: 4870

I believe that creating something like this with the Controllers won't be a problem, because they're found using namespace.

The problem is with the views.

By default the MVC routing (through the ViewEngine) only uses the Area, Controller and View values in the RouteData collection.

This is implemented in the VirtualPathProviderViewEngine in virtual FindView method(s) (and using non virtual GetPath). You'll have to override FindView methods in your ViewEngine.

It's not a very large or complicated amount of code, but your best bet is going through the source and snooping around, because there's some caching going around and some other things...

Upvotes: 1

Related Questions