
The concept of areas (
areas ) for
ASP.NET MVC is a new concept. The essence of the regions is to provide an opportunity to divide an overly large project into several groups that would continue to interact with each other. For example, the company has several development groups that create a large project that includes: the main site, the store, product reviews, a forum, an internal administration site, and others. To create such a project in ASP.NET MVC 2, we propose to separate each separate part of the project into an area, which together would continue to be one interconnected project. Separating individual project areas from a large project will allow different groups of developers to more independently develop their parts of the application.
The concept of scopes appeared in ASP.NET MVC 2 Preview 1 as scopes in a solution with several projects. After the release of ASP.NET MVC 2 Preview 2, the use of areas was enhanced by the ability to create areas in one project. Consider both options.
Areas for multiple projects
When you create a solution using work areas with multiple projects, you must follow these steps:
- create a project that will be the parent (or principal) in relation to the project-areas;
- create project areas, for example for a forum, blog, shop, etc. In these projects, you need to delete the Global.asax files, in addition, you can delete the default files created in the scripts folder, since all scripts will be taken from the parent project;
- add links to the project area in the parent project;
- create for each of the projects a class inheriting from the AreaRegistration class in which to overload two elements: the RegisterArea method and the AreaName property. RegisterArea should implement the logic for registering an area in the context of an application, the AreaName property should return the name of the area;
- in the parent project in Global.asax, you must call the AreaRegistration.RegisterAllAreas () method ; to register areas to the application context during startup;
- The final step, which is required to successfully launch a project with areas, is the modification of the project files * .csproj ( * .vbproj for Visual Basic projects) of each project. In each such file, it is necessary to uncomment the areas of the code line (shown below).
Consider the steps in a little more detail. In fig.
Figure 1 shows the structure of the solution, which contains three projects: the parent project
AreasProject and two project areas -
Forum and
Store , which implement the logic of the forum and the store, respectively.
Fig.1. ASP.NET MVC project structure with areas
')
A
ForumController controller has been created in the Forum area project, which implements three actions:
Index ,
AddPost ,
EditPost . Similarly, a
ProductsController controller with
List ,
AddReview, and
Details actions is created in the Store project. Corresponding views have been created to display the results of actions. After creating the area projects, links to these projects were added to the AreasProject project.
In fig. 1 you can see in the projects Store and Forum file
Routes.cs . This file has been added to projects in order to implement the AreaRegistration class. Consider the contents of the Routes.cs file in the Store project in Listing 1.
Listing 1. Content of the Store project Routes.cs file
namespace Store
{
using System.Web.Mvc;
using System.Web.Routing;
public class Routes : AreaRegistration
{
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Store_Default" ,
"Store/{controller}/{action}/{id}" ,
new {controller = "Products" , action = "List" , id = "" },
new string [] { "Store.Controllers" }
);
}
public override string AreaName
{
get { return "Store" ; }
}
}
}
Pay attention, the Routes class is inherited from AreaRegistration, which will allow the ASP.NET MVC subsystem to further initialize the area subsystem with it. The Routes class contains two overloaded members: the RegisterArea method and the AreaName property.
RegisterArea performs registration of routes in the context of an application with areas. Using
context.MapRoute creates a route called
Store_Default . The last value of the parameter
new string [] {"Store.Controllers"} defines the namespace in which the controllers are associated with this route.
The overloaded property AreaName returns the name of the area. This name is later used by both ASP.NET MVC and the developer to indicate the area from which the required functionality should be called.
Classes like Routes for the Store project must be created for each project in a solution with areas.
The next step is to add the next line of code in the parent project AreasProject in Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
Calling AreaRegistration.RegisterAllAreas () when starting the application will allow ASP.NET MVC to initialize all available areas and their routes in the application.
The final step required to run the project with areas is to modify the project files * .csproj (or * .vbproj, if your projects are written in Visual Basic) for each of the projects in the solution. To access these files, consistently unload projects from the solution using the
UnloadProject command . In the context menu on the uploaded project, select
Edit ***. Csproj (Fig. 2), where *** is the name of the project.
Fig. 2. Context menu allowing to edit project file
The project files contain some commented definitions that need to be uncommented to use a solution based on domains. For area projects, these are pieces of code with comments:
<! - To enable MVC area support, uncomment the following two lines: ->
and
<! - If this is an area of the child project, uncomment the following line: ->
For the parent project, you need to uncomment pieces of code marked with:
<! - To enable MVC area support, uncomment the following two lines: ->
and
<! - If this is an area parent project, uncomment the following lines: ->
Note
It is known that in future releases of ASP.NET MVC 2, the action with editing project files will be redundant. It is expected that work with areas will be supported at the Solution Explorer level, which will facilitate the creation of new areas and projects based on them.
Now that the project has been fully prepared, you can use the project area in the parent project as follows.
Let's modify the Site.Master file in the parent project as a block of tab list code so that it looks like on the fragment:
< ul id ="menu" >
< li > <% = Html.ActionLink( "Home" , "Index" , "Home" ,
new {area = "AreasProject" }, null ) %> </ li >
< li > <% = Html.ActionLink( "Forum" , "Index" , "Forum" ,
new { area = "Forum" }, null ) %> </ li >
< li > <% = Html.ActionLink( "Store" , "List" , "Products" ,
new { area = "Store" }, null ) %> </ li >
< li > <% = Html.ActionLink( "About" , "About" , "Home" ,
new {area = "AreasProject" }, null ) %> </ li >
</ ul >
Note that link building with
Html.ActionLink now requires specifying the area in which you want to find the controller and the action. If we run the project, we will see the following (Fig. 3):
Fig.3. Running application
The new tabs Store and Forum will work according to the specified rules and display views from the area projects.
Areas in one project
If in the solution of the previous example areas for several projects include the option to show hidden files for the parent project, we will see the following picture (Fig. 4).
Fig. 4. Hidden elements of the solution
In the parent project AreasProject in the
Views / Areas folder, hidden elements from the area projects are created. This is required so that the parent project independently displays the views of the project-areas.
However, this structure of hidden files can also be used directly. In ASP.NET MVC 2 Preview 2, it became possible to create solutions with single projects based on regions. Such solutions contain only one project in which all areas are implemented. In fig. Figure 5 shows the structure of a single project that implements the same areas that were implemented in the previous section with the help of several area projects.
Fig.5. The structure of a single project based on regions.
To create such a project, by analogy with the previous example, we have to create instead of several projects a hierarchy of areas in one project. To do this, you need to create an Areas folder in the root of the project, in which each created folder will be a separate area. In fig. 5 such folders are Forum and Store. They need to create Controllers and Views folders for controllers and views and implement them just as we did in the previous section. Please note that for each of the folder-areas it is also necessary to create a file Routes.cs with the implementation of the AreaRegistration class. You need to update Global.asax by adding the call to AreaRegistration.RegisterAllAreas (). After this, you will receive a project similar to the solution with several project areas.
The pleasant difference in developing a single project based on domains from a solution with several project domains is that when creating a single ASP.NET MVC project based on domains, there is no need to change its project file, since there is no need to handle dependencies between projects. In general, this somewhat simplifies the creation of a solution with areas.
The functional area is a useful mechanism that allows you to divide in a large solution the logic of the various parts into composite objects - areas. You can choose how the regions will be represented in your solution: on the basis of several projects or on the basis of only one project. In any case, the creation of areas is not difficult and will allow you to conveniently separate heterogeneous functions in the application.
Addition: solution source codes with the discussed techniques can be downloaded
from here (Visual Studio 2010 Beta 2 is required). In case the solution with several project-areas gives 404 when switching to areas, recompile two project-areas.