Code Analysis of ASP. net mvc routing matching detection component

Source: Internet
Author: User

Start learning ASP today. net mvc. net mvc Architecture and practice, we can see that there is such a component RouteMonitor. dll, I think it is quite practical. It can be used to check the ing matching of Url paths. You only need to enter the request address in the browser to obtain the matching information, A friendly page is displayed to us, as shown in:
 


Figure 1

Therefore, I decided to analyze the principle first.

1. We all know that an application starts from the Application_Start event. When creating a new ASP. net mvc application, it is added to the event by default.

RegisterRoutes (RouteTable. Routes );

Then, write some routing ing methods in the RegisterRoutes method to map the request URL to the corresponding controller.

2. Rewrite the Application_Start event to the following code:
 

1 protected void Application_Start ()
2 {
3 RegisterRoutes (RouteTable. Routes );
4 RouteDebugger. RewriteRoutesForTesting (RouteTable. Routes );
5}

 

Note that the 3rd line of code references RouteMonitor. dll components, use the static method RewriteRoutesForTesting of RouteDebugger, and assign the routing set parameters of a RouteCollection to map the page to the page in figure 1, which is convenient for viewing the routing ing.

3. Now let's analyze what is in RouteMonitor. dll.

It contains the DebugHttpHandler class, DebugRoute class, DebugRouteHandler class, And RouteDebugger class. Let's first look at what the RouteDebugger class has done:
  1 public static class RouteDebugger
2 {
3 public static void RewriteRoutesForTesting (RouteCollection routes)
4 {
5 // multi-thread synchronous access to the routing set class
6 using (routes. GetReadLock ())
7 {
8 bool flag = false;
9 foreach (RouteBase base2 in routes)
10 {
11 Route route = base2 as Route;
12 if (route! = Null)
13 {
14 route. RouteHandler = new DebugRouteHandler ();
15}
16 if (route = DebugRoute. Singleton)
17 {
18 flag = true;
19}
20}
21
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.