Published: 16 Jun 2010
By: Brian Mains

A look at ASP.NET MVC 2.0 Areas.

Contents [hide]

The ASP.NET MVC 2 Series

  • Introduction to ASP.NET MVC 2.0 In this article, we'll begin examining the new features of ASP.NET MVC 2.0 by comparing what ASP.NET MVC offers against its predecessor, ASP.NET web forms.
  • ASP.NET MVC 2.0 User Interfaces The next part to this article series on MVC 2.0 is the user interface. We saw in the last article some basics on the changes of the user interface, which we'll delve into more in this article series. Here we will begin to look at how developers can construct the view user interface.
  • ASP.NET MVC 2.0 Using Multiple Actions Brian Mains explains how to use multiple actions in ASP.NET MVC 2.0.
  • ASP.NET MVC 2.0 Templating Templating is now in the ASP.NET MVC 2.0 framework, for .NET framework 3.5 and 4.0. We are going to take a look at these features.
  • ASP.NET MVC 2.0 Attributes An overview of ASP.NET MVC 2.0 Attributes.
  • ASP.NET MVC 2.0 Validation An overview of validation in ASP.NET MVC 2.0.
  • ASP.NET MVC 2.0 Areas A look at ASP.NET MVC 2.0 Areas.
  • ASP.NET MVC 2.0 and AJAX Part 1 ASP.NET MVC makes working with AJAX really easy; it's quite impressive how powerful the framework can be. JQuery adds extra features and makes AJAX calls really easy; we'll see that all here soon.
  • ASP.NET MVC 2.0 and AJAX Part 2 In this article, we are going to look at how you can utilize AJAX in your views, and utilize a concept called unobtrusive JavaScript to take advantage of a variety of situations. ASP.NET MVC provides AJAX features very easily as we previously saw, but this comes with a caveat, and it has mostly to do with the planning of the implementation. We’re going to examine these concerns in this article.
  • ASP.NET MVC 2.0 and ADO.NET Entity Framework - Part 1 In this article, we'll examine using the ADO.NET Entity Framework (ADO.NET EF). The 4.0 version has received some major upgrades, with some major API enhancements and fixes for most of the pain points within the designer.
  • ASP.NET MVC 2.0 and ADO.NET Entity Framework - Part 2 Out of the .NET framework 4.0 comes many great enhancements, the ADO.NET entity framework (ADO.NET EF) is among the top. The framework has improved upon some of the previous bugs (in version 1), as well as enhanced the API, including many new LINQ-to-SQL-like improvements. We're going to examine using these API features to create a generic version of of a data repository.
  • Complexity in ASP.NET MVC, Part 1: Dealing With Large Models At times, every developer has a web forms page or MVC view that ends up getting pretty complex, for various reasons. Whether it pulls in and integrates a lot of data (such as a portal page) or allows users to edit a lot of information, applications require data, and the amount of data grows over time. It’s a simple reality. We’re going to examine what a larger ASP.NET MVC view looks like, and some actions we can take to keep the model instantiation code as slim as possible.
  • Complexity in ASP.NET MVC, Part 2: Plan your AJAX Carefully Brian Mains discusses management of Ajax requests in ASP.NET MVC.
  • Using DI Containers in ASP.NET MVC How to use Dependency Injection containers in ASP.NET MVC.
  • Using Microsoft Unity in ASP.NET MVC Brian Mains continues his asp.net series with the unity application block.
  • Introduction

    As the need for web application features grows, the number of files associated with a given web application can grow rapidly. In web forms applications, pages are typically separated into subfolders, with each subfolder representing a logical grouping of those pages. Web forms project templates can use any folder structure to organize files.

    ASP.NET MVC works by convention, and thus the folder structure is a little more rigid out-of-the-box; all pages are grouped into a subfolder of the root Views folder, with each subfolder matching the name of a controller in the Controllers folder. For instance, the "Organizations" controller has an "Organizations" folder beneath "Views", with all of the ASPX pages representing the views for the Organizations controller. A sample folder structure may look like the following:

    Figure 1: Sample MVC Project Structure

    Sample MVC Project Structure

    While the process can be customized a bit, the folder structure remains rigid because of the convention. A recent change to this convention adds the concept of areas, which is a balance between the rigid setup of the ASP.NET MVC framework and separation of logical components. The easiest way to see this in action is to view the folder structure. Below is the same sample project, rearranged to use inline areas.

    Figure 2: Using Inline Areas

    Using Inline Areas

    Inline project areas allow ASP.NET MVC projects to maintain the controllers, models, and views folder structure, while separating the logical components from each other; the core project implementation is separate from the Organizations area. Areas of the application can duplicate controller class names, model class names, views, etc. However, each area can also share resources in the "Shared" folder.

    You can see, in my two folder examples above, that the linguistics of the folder structures changed a bit; previously an organization controller, we now have an organization area, with another level of hierarchy in the link (the management controller appends to the URL with /management). Now requests to /Organizations/Index route to /Organizations/Management/Index.

    Routing

    The default routing mechanism between views in ASP.NET MVC is by convention. For instance, in Figure 1, to link to the Manage action would be to generate the following action link statement, which renders a hyperlink to redirect to the Organizations controller in the main project.

    Listing 1: Targeting the Manage View in the Organizations Controller

    The first parameter specifies the text of the link, while the last three parameters specify the action method to call, the controller the action method "lives" in, and the parameters of the route, which are passed like query strings (unless the parameters are specified in the route).

    The concept of areas changes this a little bit, because we need to be able to differentiate the area we want to route to. To handle this is simple; add an additional area key/value pair to the list of routing parameters. To route to our new inline area as in Listing 2, we can use the following syntax:

    Listing 2: Targeting the Manage View in the Management Controller, in the Organizations Area

    An area designation specifies the correct area to navigate to; without this, the link will navigate to the ManagementController in the main project, which doesn't exist. Adding area=”Organizations” correctly routs all requests to the controller inside the area.

    But there are a few more steps to make this happen, and this requires the use of a new object called AreaRegistration. A custom registration process must take place for areas, as in Listing 3.

    Listing 3: Registering an Area

    The RegisterArea is the key component to do the registering by adding another route to the collection of area routes. Notice the Organizations area name is hard-coded into the URL; all requests prefixed with the area name are routed specially.

    You may be wondering why we couldn't specify the Organizations part to the routing definition in the main application? If we would have added this to the standard routing table, and not through the context of the area, we still would have all of our files residing in the same project, and lose the benefits of separate project areas in the first place.

    Multi-Project Areas

    Not directly supported in ASP.NET MVC 2, ASP.NET MVC, in its beta and RC incarnations, also supported areas that were separate projects. A separate ASP.NET MVC project could be referred to as an area and work the same way just like an inline area. The approach works very similarly to inline areas, with the exception of some additional setup work within the MVC project.

    The additional setup features needed use a technique that copies the contents of the project and deploys them in the main project, essentially merging the external project area with the main ASP.NET MVC project. The deployment process is seamless to you, and happens automatically through an MSBUILD task, as part of the ASP.NET MVC futures assembly. There is a Microsoft.Web.Mvc.Build.dll assembly that performs this process.

    This isn't officially supported by Microsoft; however, if you want to attempt to utilize this setup, the following resources will help you implement the custom build task in your project:

    http://msdn.microsoft.com/en-us/library/ee307987%28VS.100%29.aspx

    http://dotnetslackers.com/articles/aspnet/a-first-look-at-asp-net-mvc-2.aspx

    As far as Microsoft is concerned, single project areas are the only type of area supported.

    Final Thoughts

    Areas are helpful to separate content, but I would recommend not going overboard, for various reasons. First, areas really add to the number of files within the project. Keeping a project's footprint as small as possible helps to keep the application maintainable. I'm not certainly advocating against areas, but am recommending a balance between project size and logical separation of content.

    One nice feature with areas is that you can setup similarly named controllers for the view and make the site feel consistent. For instance, the areas accounts, store, and catalog can consistent URLs by giving each area similarly named controllers and actions. For instance, your site can have the following:

    /Accounts/Search/Index

    /Store/Search/Index

    /Catalog/Search/Index

    And while consistency isn't a requirement, it can help users to navigate your site by having similar setup (provided each view does similar functions).

    Conclusion

    Areas help separate content, and change the way that the routing process works. Areas have their own folder structure and route their own controller requests to views. Routes require a new area routing parameter, which is used to route to the correct area.

    The ASP.NET MVC 2 Series

  • Introduction to ASP.NET MVC 2.0 In this article, we'll begin examining the new features of ASP.NET MVC 2.0 by comparing what ASP.NET MVC offers against its predecessor, ASP.NET web forms.
  • ASP.NET MVC 2.0 User Interfaces The next part to this article series on MVC 2.0 is the user interface. We saw in the last article some basics on the changes of the user interface, which we'll delve into more in this article series. Here we will begin to look at how developers can construct the view user interface.
  • ASP.NET MVC 2.0 Using Multiple Actions Brian Mains explains how to use multiple actions in ASP.NET MVC 2.0.
  • ASP.NET MVC 2.0 Templating Templating is now in the ASP.NET MVC 2.0 framework, for .NET framework 3.5 and 4.0. We are going to take a look at these features.
  • ASP.NET MVC 2.0 Attributes An overview of ASP.NET MVC 2.0 Attributes.
  • ASP.NET MVC 2.0 Validation An overview of validation in ASP.NET MVC 2.0.
  • ASP.NET MVC 2.0 Areas A look at ASP.NET MVC 2.0 Areas.
  • ASP.NET MVC 2.0 and AJAX Part 1 ASP.NET MVC makes working with AJAX really easy; it's quite impressive how powerful the framework can be. JQuery adds extra features and makes AJAX calls really easy; we'll see that all here soon.
  • ASP.NET MVC 2.0 and AJAX Part 2 In this article, we are going to look at how you can utilize AJAX in your views, and utilize a concept called unobtrusive JavaScript to take advantage of a variety of situations. ASP.NET MVC provides AJAX features very easily as we previously saw, but this comes with a caveat, and it has mostly to do with the planning of the implementation. We’re going to examine these concerns in this article.
  • ASP.NET MVC 2.0 and ADO.NET Entity Framework - Part 1 In this article, we'll examine using the ADO.NET Entity Framework (ADO.NET EF). The 4.0 version has received some major upgrades, with some major API enhancements and fixes for most of the pain points within the designer.
  • ASP.NET MVC 2.0 and ADO.NET Entity Framework - Part 2 Out of the .NET framework 4.0 comes many great enhancements, the ADO.NET entity framework (ADO.NET EF) is among the top. The framework has improved upon some of the previous bugs (in version 1), as well as enhanced the API, including many new LINQ-to-SQL-like improvements. We're going to examine using these API features to create a generic version of of a data repository.
  • Complexity in ASP.NET MVC, Part 1: Dealing With Large Models At times, every developer has a web forms page or MVC view that ends up getting pretty complex, for various reasons. Whether it pulls in and integrates a lot of data (such as a portal page) or allows users to edit a lot of information, applications require data, and the amount of data grows over time. It’s a simple reality. We’re going to examine what a larger ASP.NET MVC view looks like, and some actions we can take to keep the model instantiation code as slim as possible.
  • Complexity in ASP.NET MVC, Part 2: Plan your AJAX Carefully Brian Mains discusses management of Ajax requests in ASP.NET MVC.
  • Using DI Containers in ASP.NET MVC How to use Dependency Injection containers in ASP.NET MVC.
  • Using Microsoft Unity in ASP.NET MVC Brian Mains continues his asp.net series with the unity application block.
  • <<  Previous Article Continue reading and see our next or previous articles Next Article >>

    About Brian Mains

    Brian Mains is an application developer consultant with Computer Aid Inc. He formerly worked with the Department of Public Welfare. In both places of business, he developed both windows and web applications, small and large, using the latest .NET technologies. In addition, he had spent many hou...

    This author has published 73 articles on DotNetSlackers. View other articles or the complete profile here.

    Other articles in this category


    Code First Approach using Entity Framework 4.1, Inversion of Control, Unity Framework, Repository and Unit of Work Patterns, and MVC3 Razor View
    A detailed introduction about the code first approach using Entity Framework 4.1, Inversion of Contr...
    jQuery Mobile ListView
    In this article, we're going to look at what JQuery Mobile uses to represent lists, and how capable ...
    Exception Handling and .Net (A practical approach)
    Error Handling has always been crucial for an application in a number of ways. It may affect the exe...
    JQuery Mobile Widgets Overview
    An overview of widgets in jQuery Mobile.
    Book Review: SignalR: Real-time Application Development
    A book review of SignalR by Simone.

    You might also be interested in the following related blog posts


    Areas in ASP.NET MVC 4 read more
    Learning MVC - Part 5 Repository Pattern in MVC3 Application with Entity Framework read more
    Learning MVC Part 6: Generic Repository Pattern in MVC3 Application with Entity Framework read more
    Using areas in ASP.NET MVC to organize the project read more
    Using areas in ASP.NET MVC to organize a project read more
    Understanding the MvcRouteHandler and MvcHandler in ASP.NET MVC read more
    Code First with Entity Framework 5 using MVC4 and MVC Scaffold read more
    New Mvc4 RTM compatible Release of the Mvc Controls Toolkit read more
    Building an Senchas ExtJS 4.0 MVC Application With Microsofts ASP.NET MVC3 Series / Basics read more
    MvcContrib grid paging and searching in Asp.NET MVC3 read more
    Top
     
     
     

    Please login to rate or to leave a comment.