If you have ever worked on an application of any size, you would have gotten to a point where having every single line of code in one bucket may become overly cumbersome. Perhaps your routes have started to collide. Or perhaps, one section of your application is considerably different in form and function from the other (think about an administration console for example). These sorts of issues are easy to solve by introducing areas into your project. An area is basically an ASP.NET MVC project within another.
Areas
. Global.asax
file. Notice that there is an AreaRegistration.RegisterAllAreas()
call, which scans your application looking for any registration classes.protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
Areas
was created. Take a look in the root of your Administration
folder and you will see a file called AdministrationAreaRegistration.cs
. Open up that file and you will see that the call from the Global.asax
is actually registering the routes for your area. This allows you to specify a different set of routes for your areas from that of your root application. AdministrationAreaRegistration
class, you will notice that its default route specifies an area key along with its controller, action, and ID.public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Administration_default", "Administration/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); }
Areas are a nice and convenient way to isolate aspects of your application. This allows you to have subapplications in your application that can be controlled in a totally separate manner from your primary application. The MVC framework quickly scans the application for any classes that inherit from AreaRegistration
and then calls the RegisterArea
method on each of your area registration classes. This is a great way to keep multiple facets of a complex application within a single file structure. Prior to areas, I would have kept my Admin area (for instance) in a separate project that was then added to IIS as a virtual folder.