This article describes the role of the routing component and provides several examples to learn ASP. NET mvc2.0 URL routing technology.
Next, we started to talk about how our request was forwarded to the corresponding view after we registered a route in global. asax? How is the controller and action parsed? This is what the routing component does.
Role of routing: First, it obtains the request passed by the view and parses the controller, action, and data in the URL request, next, he passes the identified data to the Controller action (Controller method ).These are two important roles of the routing component!
The following describes the use of URL routing in several examples.
Maproute () has six methods that can be reloaded. The following are five examples!
Example 1: The default routing format provided by the system is described first.,The following is the default value provided by the system.Code:
Public static void registerroutes (routecollection routes) {routes. ignoreroute ("{resource }. axd/{* pathinfo} "); routes. maproute ("default", // route name "{controller}/{action}/{ID}", // URL with parameters new {controller = "home ", action = "Index", id = urlparameter. optional} // default value );}
URL format: http: // localhost: 0000/Home/IndexThe rule is as follows:{Controller}/{action}/{ID} is the corresponding part. The default value still exists.
The detailed match should be: http: // localhost: 0000/Custom/detials/1Remove the host domain name and match the controller and actiong. Parse the URL through the routing component. The controller is m and the action is detials. The ID passed in the past is 1.
This means that maproute (StringName,StringURL,ObjectDefaults); this method is reloaded.
Example 2: Do not use the default URL routing rule
Function header:Maproute (StringName,StringURL );
Routes. maproute ("No Default routing rule", "{controller}/{ID}-{action }");
Applicable URL example: http: // localhost: 0000/Custom/1-detials
It will not matchHttp: // localhost: 0000/
Example 3: URL routing rules with namespaces
Function header:Maproute (StringName,StringURL,String[] Namespaces); // route name, URL rule, namespace
Routes. maproute (
"Myurl", // route name
"{Controller}/{ID}-{action}", // URL with Parameters
New {controller = "home", Action = "Index", id = urlparameter. optional}, // default value of the Parameter
New String [] {"mvcdemo. controllers"} // namespace
);
URL: http: // localhost: 0000/Custom/1-detials
This example is a routing rule with a namespace, which is useful in aeras. Let's talk about it later!
Example 4: routing rules with Constraints
Function header:Maproute (StringName,StringURL,ObjectDefaults,ObjectConstraints); // route name, URL rule, default value, namespace
Routes. maproute (
"Rule1 ",
"{Controller}/{action}-{year}-{month}-{day }}",
New {controller = "home", Action = "Index", year = "2010", month = "04", Day = "21 "},
New {year = @ "^ \ D {4}", month = @ "\ D {2 }"}
);
URL: http: // localhost: 14039/home/index-2010-01-21
Example 5: routing rules with namespace, constraints, and default values
Function header:Maproute (StringName,StringURL,ObjectDefaults,ObjectConstraints,String[] Namespaces );
Routes. maproute (
"Rule1 ",
"Admin/{Controller}/{action}-{year}-{month}-{day }",
New {controller = "home", Action = "Index", year = "2010", month = "04", Day = "21 "},
New {year = @ "^ \ D {4}", month = @ "\ D {2 }"},
New String [] {"mvcdemo. controllers "}
);
URL: http: // localhost: 14039/Admin/Home/indexes-2010-01-21
Instance 6: capture all routes
Routes. maproute (
"All", // route name
"{* Vauler}", // URL with Parameters
New {controller = "home", Action = "Index", id = urlparameter. optional} // default value of the Parameter
);
Description of the remaining parts of global. asax:
Routes. ignoreroute ("{resource}. axd/{* pathinfo}"); is the URL for ignoring this rule
Arearegistration. registerallareas (); // register all areas
Registerroutes (routetable. routes); // register the rules we have written
// Routedebug. routedebugger. rewriteroutesfortesting (routetable. routes); // for debugging statements, download routedebug. dll and add references! After adding this sentence, you can test the URL routing.
First recognized ASP. NET mvc2.0 first recognized ASP. NET mvc2.0 [continued]
ASP. NET mvc2.0 URL routing entry-instance
ASP. NET mvc2.0 URL routing entry
Introduction to ASP. NET mvc3 (I) Environment preparations
ASP. NET mvc3 introduction to the ASP. NET mvc3 Project (II)
ASP. NET mvc3 simple getting started Season 1 (3) Introduction to filter in Controller
ASP. NET mvc3 simple getting started Season 1 (4) Detailed description of request processing pipeline