. Net MVC Execution Process

Source: Internet
Author: User
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
  1. Public class mvcapplication: system. Web. httpapplication
  2. {
  3. Public static void registerroutes (routecollection routes)
  4. {
  5. Routes. ignoreroute ("{resource}. axd/{* pathinfo }");
  6. Routes. maproute (
  7. "Default", // route name
  8. "{Controller}/{action}/{ID}", // URL with Parameters
  9. New {controller = "home", Action = "Index", id = urlparameter. optional} // default value of the Parameter
  10. );
  11. }
  12. Protected void application_start ()
  13. {
  14. Arearegistration. registerallareas ();
  15. Registerroutes (routetable. routes );
  16. }
  17. }
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.

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.