Are nested Areas possible in ASP.NET MVC 2?
Asked Answered
E

2

8

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.

Exemption answered 16/9, 2010 at 20:41 Comment(1)
One of the benefits of ASP.NET MVC is simpler and human-readable URLs. The more you nest areas the longer the URLs will be and the less friendly they'll be. Do you really need nesting? Just a thought (and sorry, I don't know the answer to your question).Kinsman
T
4

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.
Taxexempt answered 24/2, 2011 at 15:6 Comment(1)
+1 for helpful link, but in general answers that are just links to another site are much better when they have some additional explanatory text or at least a good relevant quote from the link. I added a quote and highlighted the relevant portion that actually answers the question.Kinsman
T
1

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...

Theolatheologian answered 24/2, 2011 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.