ASP. net mvc routing (4)
Preface
In the previous sections, we will explain how the routing system runs in MVC and its rough principles. I think I should have a conceptual understanding of the routing. In this article, I will explain the region, after reading this article, you will not be sure to have a thorough understanding of the region, but it will help you lay a solid foundation on your future knowledge of this part.
Region
At the beginning of the routing, the MVC project created in the VS environment is used. Today, the region also follows this routine. On MSDN, MVC divides large projects into smaller units, that is, regions. I have no experience in using regions, therefore, this article only describes its object model and its rough principle.
(Create a project without an MVC project.) In the MVC project, right-click the project and choose add> region. Then, in the displayed dialog box, enter the region name,
After you click Add, you will find this directory structure in the project,
Regardless of the meaning of these directory structures, let's take a look at the code for registering a region in the Application_Start () method of the MvcApplication class in the Global. asax file mentioned above:
1 AreaRegistration.RegisterAllAreas();
The RegisterAllAreas () method is used to register all regions. The process is as follows:
When the RegisterAllAreas () method is executed, All cached region types (all implement AreaRegistration) are read internally Based on the MVC-AreaRegistrationTypeCache.xml region cache file. If the cache file is empty, the system traverses all the Assembly dependent on the current system and searches for the AreaRegistration type and writes it to the cache file.
And return a type set. At this time, the type set implements the implementation class of the AreaRegistration type. When traversing each type, it will reference and pass the current system's route table RouteTable. routes is passed to the new object AreaRegistrationContext (parameter context object), and then the abstract method RegisterArea () of the base class AreaRegistration is called. Of course this is implemented in the Framework, in actual situations, the base class AreaRegistration is already the AreaCaseAreaRegistration type in the preceding example.
Here, let's take a look at the internal implementation of the AreaCaseAreaRegistration type:
1 public class AreaCaseAreaRegistration : AreaRegistration 2 { 3 public override string AreaName 4 { 5 get 6 { 7 return "AreaCase"; 8 } 9 }10 11 public override void RegisterArea(AreaRegistrationContext context)12 {13 context.MapRoute(14 "AreaCase_default",15 "AreaCase/{controller}/{action}/{id}",16 new { action = "Index", id = UrlParameter.Optional }17 );18 }19 }
The MapRoute () method in the AreaRegistrationContext type in the RegisterArea () method is not a method in the RouteCollectionExtensions extension type, but a layer is encapsulated outside of it. Here, MapRoute () the DataTokens attribute value "area" in the route object registered by the method is the region of the secondary route, that is, the region name.
At this time, we can find through debugging that the routes in the region are also registered with the routes defined in the program in the routing table, indicating the division of the region, that is, the routes defined on the physical file, of course, there are other major functions that I have not realized for the moment. I will share with you as soon as I feel it.
Author: Jin Yuan
Source: http://blog.csdn.net/jinyuan0829
The copyright of this article is shared by the author and CSDN. You are welcome to repost this article, but you must keep this statement without the author's consent.