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