ASP.NET MVC En Masse – Areas

So I’m embarking on my first really large ASP.NET MVC project and I’m really excited about the prospects. But, with any new programming model comes change and little snags that you didn’t see coming. So I’m going to try and blog about these as the project progresses so others can see how we handled such obstacles and to help reinforce the solution in my own mind.

The first thing that you’ll run into in any large MVC application (and I suspect this would happen with Rails or Django as well) is that you end up with a lot of controllers…I mean…a lot of controllers. Inevitably these controllers revolve around similar functionality but you can’t quite bring yourself to combine them because doing so would create uber-controllers that break single responsibility and become unmanageable in their own right. The solution of course is to inject some conceptual node above the controller layer…in essence you want to group controllers.

So after doing some digging I found an excellent blog post by Phil Haack (the instigator of ASP.NET MVC at Microsoft). I think Phil is great, he’s stuck it to the “man” with an great project that beats WebForms hands down. Anyway, in his post he describes a mechanism for extending routing so that you can add essentially another layer to the application above controller, he calls them “areas” but I don’t think it really matters what you call them

If you download the source code for his prototype you’ll see that it’s basically a standard MVC project with just a few small modifications.

  • AreaRouteHelper.cs – This guy’s job is to add areas to route mapping.  It allows you to map routes inside areas and also map routes to the “root” or controllers that aren’t in an area.
  • AreaViewEngine.cs – this just adds patterns so that aspx/ascx views can be found one more level down in the file structure.
  • Changes to the global.asax to register the new area engine.
  • Changes to how the route table is set up (using AreaRoutHelper methods)

It’s super simple to set up and a really effective way to extend the MVC structure.

Category: ASP.NET MVC Comment »


Leave a Reply



Back to top