Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Creating MVC "Areas" as Subfolders under Views

The support of "areas," or groups of controllers and views organized into modules, in a medium or large sized application is absolutely vital. It seems quite bewildering to me that Microsoft intends on releasing ASP.NET MVC 1.0 without built in support for areas.

Fortunately, what ASP.NET MVC framework lacks in capability is made up with extensibility.  It really is quite extensible, allowing you to replace parts of it as needed.  For example, for S#arp Architecture, I took guidance from Phil Haack and Steve Sanderson but modified the merged result to support areas directly under the Views folder.  (I didn't like that there was a separate "Areas" folder to hold your areas; granted, their areas assumed that they'd also be holding the controller for each area as well.)  The result supports subfolders under the Views folder which reflects the namespace of your model.  For example, if you create a domain object namespaced as Store.Core/Warehouse.Inventory.Product with a corresponding controller Store.Web.Controllers/Warehouse.Inventory.ProductsController, your Views folder would reflect Store.Web/Views/Warehouse/Inventory/Products.  You could then browse to http://YourServer/Warehouse/Inventory/Products to see a listing of products.

This was only possible due to the fact that much of ASP.NET MVC is "replaceable" with your own parts.  For example, as Haack suggested, you can write your own view engine to handle the matching of requests to views, per the needs of the application.  A working example from S#arp Architecture best illustrates.  (Even if you're not interested in using S#arp Architecture, I would encourage you to check out how areas were implemented in it for use in your own application.)  The relevant code includes:

With the above, you'd be able to browse to http://YourServer/Organization/Employees/Index and the views would be hierarchically organized under the Views folder.  Incidentally, S#arp Architecture includes a CRUD scaffolding generator which takes into account nested namespaces automatically.

Although it's too bad that ASP.NET MVC won't support areas natively, it's pleasent to realize just how customizable the entire MVC framework really is, to allow us to fill in any missing gaps ourselves.

Billy McCafferty


Posted 01-22-2009 4:42 PM by Billy McCafferty

[Advertisement]

Comments

Casey wrote re: Creating MVC "Areas" as Subfolders under Views
on 01-23-2009 3:28 AM

Great stuff on this Billy (and team), this was a major failing in my last project an took a disproportionate amount of time to resolve - we never got a decent solution in place

Scanning your code, and it looks like just the ticket - I'm gonna try and give it a go next week on a sample project.

Thanks!

Reflective Perspective - Chris Alcock » The Morning Brew #271 wrote Reflective Perspective - Chris Alcock » The Morning Brew #271
on 01-23-2009 3:42 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #271

Dew Drop - January 23, 2009 | Alvin Ashcraft's Morning Dew wrote Dew Drop - January 23, 2009 | Alvin Ashcraft's Morning Dew
on 01-23-2009 10:37 AM

Pingback from  Dew Drop - January 23, 2009 | Alvin Ashcraft's Morning Dew

DotNetShoutout wrote Creating MVC "Areas" as Subfolders under Views - Billy McCafferty
on 01-23-2009 11:36 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

ASP.NET MVC Archived Blog Posts, Page 1 wrote ASP.NET MVC Archived Blog Posts, Page 1
on 01-25-2009 11:44 PM

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

Mike wrote re: Creating MVC "Areas" as Subfolders under Views
on 02-23-2009 8:11 AM

I like it better this way to, feels more natural. Thanks for the effort, I'm using it on a real project, seems to hold up just fine.

But you know the official areas support will insist on grouping Views, Controllers en other related content together because it makes for a clean 'plugin' model: just drop a folder into Areas, map the routes (or auto-map based on convention) and good to go...

Billy McCafferty wrote re: Creating MVC "Areas" as Subfolders under Views
on 02-23-2009 8:29 AM

It all depends who or what is doing the plug-in. ;)  For example, S#arp Architecture has its own plug-in model which assumes areas to be under the views folders.  That's the nice thing about MVC; you can replace some pieces with new capabilities.

Your New Toy ASP.NET MVC 1.0 is Ready !!! | Semantic Web wrote Your New Toy ASP.NET MVC 1.0 is Ready !!! | Semantic Web
on 03-18-2009 11:47 AM

Pingback from  Your New Toy ASP.NET MVC 1.0 is Ready !!! | Semantic Web

Ian wrote re: Creating MVC "Areas" as Subfolders under Views
on 04-14-2009 11:11 AM

ExpressionHelper is no longer available for some reason does anyone know where its been moved to/replaced with.

David Perlman wrote re: Creating MVC "Areas" as Subfolders under Views
on 05-04-2009 5:03 PM

This is a great addition to the already super SharpArch. Still I think I should mention an issue I had.

It seems that although ViewEngines.Engines.Add(AreaViewEngine( ) );

would call the constructor of AreaViewEngine, later calls to FindView would still default to the System.Web.Mvc.ViewResult.FindView().

My workaround was to place the completely independent AreaViewEngine class in my .Web project. It's now working fine.

Any hints on how to solve this?

Thanks,

David

Billy McCafferty wrote re: Creating MVC "Areas" as Subfolders under Views
on 05-05-2009 9:21 PM

I've also noticed the problem you describe.  Can you describe your resolution in more detail - I'm hoping to get ideas from it. ;)

Thanks!

Pharmk140 wrote re: Creating MVC "Areas" as Subfolders under Views
on 04-23-2010 11:01 AM

Hello! kegefgk interesting kegefgk site!

Pharmd257 wrote re: Creating MVC "Areas" as Subfolders under Views
on 05-07-2010 10:42 PM

Hello! fdddebd interesting fdddebd site!

Pharma986 wrote re: Creating MVC "Areas" as Subfolders under Views
on 05-07-2010 10:47 PM

Hello! dkedegb interesting dkedegb site!

Pharmb523 wrote re: Creating MVC "Areas" as Subfolders under Views
on 07-15-2010 8:45 AM

Hello! ffdacge interesting ffdacge site!

Pharmb356 wrote re: Creating MVC "Areas" as Subfolders under Views
on 08-18-2010 5:53 AM

Hello! aggkdde interesting aggkdde site!

weatherbyf wrote re: Creating MVC "Areas" as Subfolders under Views
on 09-28-2010 11:50 AM

natural led heat

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)