New Concept of ASP. NET MVC2 areas Region

Source: Internet
Author: User
Tags actionlink
To facilitate the management of a large number of files on a large-scale website, ASP. net mvc 2.0 introduces a new concept, area ). In this article, we will discuss basic operations related to the Section concept.

WithProgramWith the increasing functional requirements, the number of files associated with specific Web applications may increase dramatically. In web forms applications, a webpage is usually divided into several subfolders, each of which represents a logical group in these Web sets. On the other hand, the web form project template may use any folder structure for file organization.

ASP. net mvc is managed according to established practices. Therefore, the corresponding folder structure is relatively strict. You can even copy and use it. All webpages are divided into some subfolders under the root folder views, each sub-Folder matches the name of each controller in the controllers folder. For example, the "organizations" controller has a corresponding "organizations" folder under the "views" folder. All aspx webpages describe the views corresponding to the organizations controller. Figure 1 shows a possible sample folder structure:

Figure 1-sample MVC project structure

Although the above process can be slightly customized, the folder structure remains strictly organized due to conventions. In the latest version of MVC 2.0, a new concept of area is added to the above conventions. In this way, there is a balance between the strict ASP. net mvc Framework and the separation of logical components. The simplest way to understand this is to observe the folder structure of the next sample project. The following shows the same project as Figure 1, but the layout is re-deployed using the inline section technology.

Figure 2-using inline segments

The Inline project section allows ASP. while maintaining the structure of controllers, models, and view folders, the net MVC project also separates logical components from each other. However, the core project implementation and Organizations Sections are separated. The Application Section can contain repeated controller class names, model class names, and view names. However, each section can share resources in the shared folder.

As you may see, in the two folder architecture examples shown in Figure 1 and figure 2 above, the folder architecture is slightly changed. In Figure 1, there is only one organization controller, and now there is one organization segment, and another level of hierarchy appears in the connection. Now, requests for/organizations/index are routed to/organizations/management/index.

I. Routing

The default routing mechanism used between views in ASP. net mvc is implemented by convention. For example, in Figure 1, a link to manage behavior generates the following behavior link declaration, which generates a hyperlink redirected to the organizations controller in the main project.

Listing 1-locking the target on the manage view in the organizations Controller

Html. actionlink ( " Manage this Organization " , " Manage " ,
New {Controller = " Organizations " , Key = 1 })

 

The first parameter specifies the link text, and the last three Parameters specify the behavior method to be called, the controller where the behavior method is located, and the route parameters, these parameters are passed like query strings (unless the parameters are specified in the route ).

The introduction of the section concept has made some changes to the above situation, because we need to be able to distinguish the sections to be routed. To achieve this, we only need to add the key/value pairs corresponding to an additional segment to the route parameter list. To route to the new inline segment shown in Listing 2, we can use the following syntax:

Listing 2-locking the target on the manage view in the management controller on the Organizations Section

Html. actionlink ( " Manage this Organization " , " Manage " , " Management " ,
New {Area = " Organizations " Key = 1 })

 

The target of a CIDR Block specifies the correct CIDR block to be navigated. If this method is not used, the link will navigate to the Manager controller in the main project, but this controller does not exist. AboveCode, We add area = "organizations" to route all requests to the controllers in the zone range.

 

However, to achieve this goal, you still need several steps-you need to use a new object called arearegistration. We need to use a custom registration process for the Section, as shown in listing 3.

Listing 3-sample code for registering a region

 

Public Class organizationsarearegistration: arearegistration
{
Public Override String Areaname
{
Get
{
Return " Organizations " ;
}
}
Public Override void registerarea (arearegistrationcontext context)
{
Context. maproute (
" Organizations_default " ,
" Organizations/{controller}/{action}/{ID} " ,
New {Action = " Index " , ID = Urlparameter. optional}
);
}
}

 

Here, registerarea is a key component for registration. It is implemented by adding another route to the segment routing set. Note that the section name organizations is added to the URL in hard encoding mode. Therefore, all requests that use the section name as the prefix are specially routed.
You may wonder: why cannot we specify the Organizations Section in the routing definition of the main application? In fact, if we add it to the standard routing table without passing the section context, we will still store all the files in the same project, in this way, the benefits of Separating project segments are lost (that is, failed project segments ).

Ii. Multi-Project Section

Although not directly ASP. net MVC 2 supported, but in the test version of ASP.. Net MVC still supports multi-project segments. A separate ASP. net mvc project can be considered as a segment, and its work is like an embedded segment. This method is very similar to the embedded Engineering Section. Of course, you need to perform some additional settings in the MVC project.

The additional setup feature above requires the use of a technology, that is, copying the project content and deploying them in the main project, mainly to merge the external project area with the main ASP. net mvc project. With the help of ASP. net mvc, the framework will automatically complete the future Assembly for you through a msbuild task. An assembly called Microsoft. Web. MVC. Build. dll is responsible for executing this process.

Note: These functions are not supported by Microsoft. However, if you want to use this installer, the following resources will help you implement custom Project Build tasks:

Http://msdn.microsoft.com/en-us/library/ee307987%28VS.100%aspx
Http://dotnetslackers.com/articles/aspnet/a-first-look-at-asp-net-mvc-2.aspx

As for Microsoft, only one project segment is supported so far.

Iii. my views

The introduction of the "segment" concept helps to achieve content separation, but for various reasons, I suggest not to over-use this concept. First, the introduction of "segment" does increase the number of files within the project scope. Keeping the number of project files as small as possible will help to maintain the maintainability of the application. Of course, I am not opposed to the use of the segment technology, but it is recommended to maintain a moderate balance between the project size and the logical separation of content for the best.

A good feature of using the section is that you can set a controller with a similar name for the view to make the entire website feel consistent. For example, you can use the same URL for the region accounts, store, and catalog by giving a controller and behavior similar to the name of each region. For example, your website can be organized as follows:

 

/ Accounts / Search / Index
/ Store / Search / Index
/ Catalog / Search / Index

 

 

Although consistency is not necessary, it helps users navigate to your website more easily-by using similar settings (assuming that each view provides similar functions ).

Iv. Conclusion

The introduction of the "segment" concept helps to achieve content separation and change the way the routing process works. The Section has its own folder structure and can route its own Controller requirements to the corresponding view. A new CIDR block routing parameter is required for routing to the correct CIDR block.

From: http://tech.it168.com/a2010/0901/1098/000001098030_1.shtml

Related Article

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.