WEBFORM/MVC Source Code Analysis

Source: Internet
Author: User

Browser Url:https//localhost:6565/home/index, https//localhost:6565/webform1.aspx, request server (build request message and send request message to server)

1: Server (kernel-mode HTTP. sys) makes basic processing of request messages

2: Request Server (user mode, IIS server)

2.1: Do a check on the data sent, if the request is a static resource (. Html,jpg,js), then IIS will directly return these resources to the browser

2.2: If it is a dynamic resource (. aspx,.ashx), the IIS server cannot process directly, requesting the W3wp.exe process (unmanaged mode/managed mode)

2.3: Unmanaged mode (aspnet_isapi.dll), load the row, and then pass the data. NETFramework, then go into managed mode

  (The above steps cannot be verified by code, Microsoft does not open source, the following steps can be viewed through reflector source code)

2.4: Managed mode (isapiruntime), a very important method

  public int ProcessRequest (IntPtr ECB, int iwrtype) //ECB handle: Is the concept of the operating system, refers to the resource number, the operating system adds all the resources to the corresponding number.

Reflector 700 Degree Cloud disk: Link: http://pan.baidu.com/s/1eSm9Y06 Password: gyx0

1: Search Isapiruntime with Reflector

2: View the ProcessRequest method inside, create a isapiworkerrequest based on the ECB handle (encapsulates the most primitive data of the request packet)

3:httpruntime.processrequstnodemand (WR); Passing HttpWorkerRequest to httpruntime

4:processrequestnow method

5:processrequestinternal method

HttpContext (Context=new HttpContext (Wr,false)) was created in httpruntime; The data for the final request packet is encapsulated in the HttpContext (Httprequest/httpresponse)

5.1: Initialize in HttpContext class : New HttpRequest (wr,this);//Receive Data

New HttpResponse (wr,this);//Output data

GetApplicationInstance method in 5.2:processrequestinternal (after initialization of the HttpContext class)

Created a HttpApplication object that handles the data sent by the browser, as the data for the request packet is encapsulated in the HttpContext,

So where do you need to pass the HttpContext to HttpApplication, and how is HttpApplication created?

IHttpHandler applicationinstance = httpapplicationfactory.getapplicationinstance (context); Is the HttpApplication created by the HttpApplicationFactory factory.

In the creation of HttpApplication, the use of the application of the HttpApplication pool, in the creation of the first look at the pool there is no idle HttpApplication, if there is a direct take out the use, and then put into the pool.

Here the pool is the stack (stack) and if there is no idle in the pool, create one.

The 1:httpapplication object can only process one request at a time. The life cycle of the HttpApplication object here is only for requests with dynamic content, and static content IIS directly handles it for us.

2: And for the other 19 events in HttpApplication, it is triggered once for each request (dynamic content is requested).

3:application_start is triggered once when the user requests the dynamic content for the first time (in real IIS).

Application_End at the end of the application, such as restarting iis/restarting the site/restarting the application pool for the corresponding site, and ending the w3wp.exe process.

6: View IHttpHandler applicationinstance = httpapplicationfactory.getapplicationinstance (context);

6.1 theapplicationfactory.ensureinited ();

6.2 theapplicationfactory.ensureappstartcalled (context);

_theapplicationfactory.ensureappstartcalled (context); Make sure that the Application_Start method in the global file is called and called only once.

6.3 Return _theapplicationfactory.getnormalapplicationinstance (context);

State= (HttpApplication) httpruntime.createnonpublicinstance (This._theapplicationtype);

(Create a HttpApplication of the type of the global file compiled by reflection)//Create HttpApplication Object

Locate the Global.asax file and compile the Global.asax file, compile the _theapplicationtype, and invoke the [_theapplicationfactory.ensureinited ();]

7:state. Initinternal (context, this._state, this._eventhandlermethods);

8:this. Initmodules ();

When HttpApplication is created, the information about HttpModule is traversed in the Web. config file, and then the Init method is executed.

Initmodulescommon

We're going to go into the pipeline event model (application )

Starting with HttpApplication object creation, calling the ProcessRequest method to the end of the call, the entire process is called "ASP." NET Application life cycle "(not the life cycle of an ASP.)

"Handling user requests: Triggers 19 events, 23 steps"

9: The 7th event (the difference between MVC and WebForm) is reached, and the code in the method is executed through the EventHandler delegate call Onapplicationpostresolverequestcache.

9.1:asp.net WebForm

A user-requested ASHX or ASPX page object is created between the 7th event and the 8th event in the life cycle of an ASP.

In the case of an ASPX page, the object of the page created here is not an object of the ASPX-corresponding background class, but an object of the class generated by the ASPX page, which inherits from the background code file.

So creating an object of that type has everything in the background code file.

"Not to be Continued"

9.2:asp.net MVC

Here is a very important module is Urlrouteingmodule (the Init method in Urlrouteingmodule)

In the Init method in Urlrouteingmodule, you define:

Application. Postresolverequestcache+=new EventHandler (this. Onapplicationpostresolverequestcache);

1:[7th to 8th event] arrives at the 7th event, and the code in the method is executed through the EventHandler delegate call Onapplicationpostresolverequestcache

2:[11th to 12th event] begins execution of the ProcessRequest method in Mvchandler

ASP. NET MVC Source analysis:http://www.cnblogs.com/Dr-Hao/p/5315556.html

WEBFORM/MVC Source Code Analysis

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.