1. url Route Comparison
2. Execute controller and action
3. Execute the view and return the result.
When MVC is used, it is successfully compared by the ignoreroute () auxiliary method, which will cause the program to jump directly from the MVC execution lifecycle and hand over the program's right to continue executing to IIS, it is up to IIS to decide which module or processing routine to execute.
Phase |
Details |
Receives the application's first request |
In the global. asax file,RouteObjectAddedRoutetableObject. |
Select routes |
UrlroutingmoduleThe module uses the firstRoutetableSetRouteObject To createRoutedataObject, and then it will use thisRoutedataObject To createRequestcontext(Ihttpcontext) Object. |
Create an MVC request handler |
MvcroutehandlerCreateMvchandlerClass, and pass itRequestcontextInstance. |
Create a controller |
MvchandlerObject usageRequestcontextInstance to confirmIcontrollerfactoryObject (DefaultcontrollerfactoryTo create a conteoller instance. |
Execute Controller |
MvchandlerThe instance calls the Controller's execution method. |
Call action |
Most controllers inherit fromControllerBasic class. Associated with the ControllerControlleractioninvokerThe object determines which method of the controller class will be called and then calls that method. |
Execute result |
Most controllers inherit fromControllerBasic class. Associated with the ControllerControlleractioninvokerThe object determines which method of the controller class will be called and then calls that method. |
Routing:
Routing indicates URL routing, which plays a role in MVC:
1. Compare HTTP sent through the browser;
2. Return the appropriate website address to the browser;
If httphandler is handled by mvchandler, it will enter the execution declaration cycle of MVC, find the appropriate controller and action to process it, and feedback the information to the client.
In other words, the role of the routing module is to map incoming browser requests to the unique MVC controller actions.
Define URL routing in global. asax
C # code
- Public class mvcapplication: system. Web. httpapplication
- {
- 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 of the Parameter
- );
- }
- Protected void application_start ()
- {
- Arearegistration. registerallareas ();
- Registerroutes (routetable. routes );
- }
- }
Public class mvcapplication: system. web. httpapplication {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} // parameter default value);} protected void application_start () {arearegistration. registerallareas (); registerroutes (routetable. routes );}}
A. All ASP. NET web applications run the application_start () method of httpapplication. When this method is called, The registerroutes () method is called in turn. Routetable. routes is a public static object used to store all routing rule sets (routecollection class)
B. The ignoreroute auxiliary method is used to define URLs that do not need to be processed by routing, and to set URL paths in *. axd and other formats that do not run through MVC.
C. default is the route name. Note that the URL cannot start.
If you type/home/index/3 in the address bar of the web browser, the default route maps the URL to the following parameters:
Controller = home |
Controller name |
Action = index |
Controller action |
Id = 3 |
Id Parameters |
When you request a URL such as/home/index/3, the following code will be executed. Homecontroller. Index (3 ). The default route contains three default parameters. If you do not provide a controller, the default controller is home. Similarly, action is index by default, and ID is a null string by default.
Of course, sometimes we need specific routing requirements. In that case, we need to create a custom route.
Finally, the order is very important. The more advanced, the more called.