MVC area-related technologies

Source: Internet
Author: User
Tags actionlink

Transferred from: http://www.cnblogs.com/zgqys1980/archive/2012/08/22/2650774.html

in ASP. NET MVC, it relies on the fixed naming rules of some folders and classes to organize the model entity layer, the Views view layer and the control layer. For large-scale applications, which often consist of modules of different functions, each of which is comprised of three layers in MVC, it is sometimes a burden to developers to organize the directory structure of the MVC three layer in these different functional modules as the size of the application grows.

Fortunately, ASP. NET MVC allows developers to divide the application into the concept of "area", each of which names the file directory structure and the naming rules of the class according to the rules of ASP. In this article, you will learn how to use area for module management in an ASP. NET MVC application.

What is areas?

To put it simply, areas is the directory structure and naming method that uses ASP. NET MVC rules for each function module by dividing the ASP. NET MVC application into different functional modules. Consider a scenario like this:

As you can see, the application is comprised of three functional modules, Blog,help desk and shopping, respectively. If you do not use the zone areas, you must put all the control layer and the view layer files in their respective directories, obviously, can not be in different functional modules of the controller has the same name, such as can not be named in the blog module HomeController, The Helpdesk module is also named HomeController. The workaround is to put all the action methods in all modules together in one controller, or create two controllers, named in different ways (Bloghomecontroller and Helpdeskhomecontroller).

If areas is used for module partitioning, then each function module replicates the MVC directory structure. For example, each module will have its own control layer, the view layer, and the entity layer's directory. Therefore, you can have the HomeController class in the blog module, you can also have the same name in the Helpdesk module HomeController class. So, actually in the above example, there will be 4 MVC structure, one is the main program, three are three modules (Blog, HelpDesk and shopping)

Add a new Area

Let's start by learning how to add area. Start by using Vs.net 2010 to create a new MVC application. Then in the scenario resolver, when you right-click and select New >area in the menu that appears, you will see a dialog box like this:

Enter the name of the area you want to add, such as Helpdesk. After entering three different area, the project renders the structure as follows:

Can see clearly, the entire application is a directory called areas, wherein the following three modules have their own control layer, the model layer and the view layer of the directory. Similarly, in the outer directory of the application, there is still a directory of the entity layer and the control layer and the view layer.

Registering an area in the MVC framework

In addition to building a directory structure, it is also necessary to tell the ASP. NET MVC Framework area has been established, this is a registration step, fortunately, in the establishment of a new area has been automatically built up. Please note that in each area directory, a registered class file (such as BlogAreaRegistration.cs, HelpDeskAreaRegistration.cs) is automatically generated. Each area of the registration class file is inherited from Arearegistration this base class, such as the Helpdeskarearegistration class file code is as follows:

public class Helpdeskarearegistration:arearegistration
{
public override string AreaName
{
Get
{
return "HelpDesk";
}
}
public override void Registerarea (AreaRegistrationContext context)
{
Context. MapRoute (
"Helpdesk_default",
"Helpdesk/{controller}/{action}/{id}",
New {action = "Index", id = urlparameter.optional}
);
}
}

As you can see, the Helpdeskarearegistration class has covered the AreaName property and the Registerarea method. The Registerarea method registers the new routing information in MVC.

There must be a registration class like this in each area. But when to use these registered classes? If you open global.asx this file, you will find the following code found in the Application_Start event:

protected void Application_Start ()
{
Arearegistration.registerallareas ();
Registerglobalfilters (globalfilters.filters);
RegisterRoutes (routetable.routes);
}

Here the reader can see that the static method of calling the Arearegistration class Registerallareas () goes to register all the is registration files, and Registerallareas () Method invokes the Registerarea () method of all area in the application.

Next, add HomeController to the main program and each area, so that there will be four control classes named HomeController, as follows:

public class Homecontroller:controller
{
Public ActionResult Index ()
{
return View ();
}
}

Similarly, right-click each index () method to add a new index view to the menu that pops up, so there are 4 index view pages in total. To run the app, you can see the effect, which is the effect of running Helpdesk area, notice the URL

 Calls between areas

in ASP. NET MVC, it is often necessary to call each other between different methods of the control layer. If not specifically specified, the default is the call between the action method and the controller in the same area. If you need to call each other between different area, you can use the following methods:

<%= Html.ActionLink ("Main area", "Index", "Home", new {area = ""}, NULL)%>
<br/><br/>
<%= html.actionlink ("blog area", "Index", "Home", new {area = "blog"}, NULL)%>
<br/><br/>
<%= Html.ActionLink ("Help Desk area", "Index", "Home", new {area = "HelpDesk"}, NULL)%>
<br/><br/>
<%= Html.ActionLink ("Shopping area", "Index", "Home", new {area = "shopping"}, NULL)%

As you can see, the above uses the ActionLink () method to generate the link, note the 4th parameter, use the parameter in the form of new {area= "Blog"}, and indicate which area of the action method is called.

Using Redirecttoaction

Similarly, we often want to invoke the action in an area method by the action method in an area, how do we do this? The code is as follows:

Public ActionResult Index ()

{

Return redirecttoaction ("Index", "Home", new {area = "HelpDesk"});

}

Here, the Redirecttoaction method is used to invoke the action method in the other area, and the name of the area is specified using the new {area= "HelpDesk"}, so the HelpDesk is called here. The index () method in area.

 Summary

In this paper, the concept of area in ASP., area modularization, can be divided into various modules, and in each module can be in accordance with the MVC architecture of the view, entity and control layer of the directory architecture, which is more conducive to the organization of the project structure, Clearer and easier to make corresponding calls between the modules.

(EXT) MVC area-related technologies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.