Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ASP.NET MVC2 Areas

DZone's Guide to

ASP.NET MVC2 Areas

·
Free Resource

The second release of ASP.NET MVC brought about some nice changes and additions. One of them that is extremely cool but can get overlooked is support for areas. An area is a new way to add some separation to your application by breaking it down into logical sections. The area has it's own set of controllers, models and views and even it's own route configuration. A common area is to have one for "Admin" where you would put any administration side code. Using an area for this allows you to use one application, but have logical separation so that is almost feels like it's two sites. I have built sites before that have a consumer and a professional side to them and area can really make it easier to keep those sections separate. Two developers can work on the different sections simultaneously with almost no overlap.

To add an area to your application is very easy. Right click on the project and select "Add Area":



Give the area a name, like "Pro":

You will see that you now have an "Areas" folder (if you didn't already) and there is a folder under that for the area itself.



You can see that the area has it's own controllers, models and views just like a normal ASP.NET MVC application has.

You can also manage your routes on an area by area basis by using the newly created ProAreaRegistration class:

public class ProAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Pro";
}
}

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Pro_default",
"Pro/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}

You can register the routes for just this area with in this class using the context.MapRoute method, just as you would in the Global.asax for your normal routes.

As you can see, areas can definitely make your ASP.NET MVC application easier to manage and maintain without having to do a lot of extra work.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}