Using areas to separate application components

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.

How to do it...

  1. For consistency, I'm going to start off with a copy of the previous recipe. But there is dependency on what has come before, so you should be able to work with an empty project just as easily.
  2. Right-click on your project and select Add New Area.
  3. Name your area Administration. This will add a new Administration area to a folder called Areas.
  4. Now open up your 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);
    }
    
  5. When you created the Administration area, a new folder structure called 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.
  6. Notice that the route definition for your area is a bit different from that of the routes configured for the rest of your application. In the 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 }
    );
    }
    

How it works...

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.

There's more...

Be sure to also take a look at the fact that areas don't have to be created inside your application. You can also create areas in separate projects and still register them inside your primary application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset