ASP. NET web page request and processing process (decompilation tool to view source code)

Source: Internet
Author: User

This article is a personal summary after you view the source code, and does not guarantee its accuracy. For your reference.

Communication between the browser and the server.

When you knock a domain name on the browser, and then press enter, such as: http://www.baidu.com/index.aspx

The browser generates the corresponding request message according to the HTTP syntax.

The browser checks whether the server IP address corresponding to the http://www.baidu.com/index.aspxdomain name is saved on the local machine. If no, send a request to the nearest DNS server (domain name resolution server) in the city network. It will query the IP address of the server corresponding to this domain name based on the domain name we sent, and send it back to the browser.

The browser DNS server obtains the IP address of the server computer corresponding to this domain name, and then finds the corresponding server in the huge Internet.

The HTTP Protocol specifies that the default port used by server software such as IIS is port 80, that is, the browser sends the HTTP request packet to port 80 of the corresponding server by default.

 

The server receives the HTTP request message sent by the browser (use httpwatcher to view the specific message ).

The following is the request message sent to Baidu:

 

 

The server analyzes the path and file name in the request message and finds the file on the server.

For CSS, HTML, JS, images, and other files, read the file directly on the server and send it to the browser.

Client.

Processing of ASP. NET Dynamic Pages

When IIS on the server finds that the page you requested is a dynamic page, it finds that it cannot be processed by itself.

Open IIS and you will find a process in itProgramIng

 

 

 

That is to say, IIS will hand over the files with the. aspx suffix to the corresponding processing program (aspnet_isapi.dll ). ISAPI.

The ISAPI sends the request to. NET Framework.

In addition, isapisubmits the request to asp.net, and also calls the class ------- isapiruntime in the framework by calling aspnet_wp.exe.

The following is the class isapiruntime analyzed by decompilation tool.

 

 

 

 

When you see this method, you will think of garbage collection.

In fact, this is not the focus. Focus:

This processrequest Method

 

Click this method

 

 

First, an isapiworkerrequest object WR will be created to encapsulate the request packet into Wr.

Call the createworkerrequest method of this class to instantiate this object.

Enter this method

 

 

This method creates different objects based on the current IIS version.

Return to the processrequest method.

 

 

Then the processrequestnodemand method of httpruntime is called. Upload WR

 

Enter this method

 

At the end of this method

Processrequestnow method. Process requests

This method calls another method. Go in

 

 

Here we will find a familiar thing, httpcontext (context object)

This method creates an httpcontext Based on the created isapiworkerrequest object WR (which encapsulates request packets. If an error occurs during creation, a 400 error is returned.

 

 

Before determining whether it is the first request

 

 

(For the first request, set the current time to the start time of the first request. Initialize the first request. Set the first request to false)

 

Initialize response.

 

When httpwriter is empty, it is created. We can see that there are two writers in context. response.

 

One is httpwriter and the other is textwriter.

Then create an httpapplication object through httpapplicationfactory (this object is responsible for truly processing the creation and execution of the page object, first in the httpapplication pool to see that there is no such object, there is no new)

Next, open the httpapplication class.

 

 

The most important event is the 25 events. 19 of these events are open to us.

1, beginrequest

The beginrequest event is triggered when the HTTP pipeline starts processing requests.

2-3, authenticaterequest, postauthenticaterequest

ASP. NET has successively triggered these two events, enabling the Security Module to authenticate the request ,.

4-5, authorizerequest, postauthorizerequest

ASP. NET has successively triggered these two events, enabling the Security Module to authorize the request process.

6-7, resolverequestcache, postresolverequestcache

ASP. the cache module uses the cache to directly respond to the request directly (the cache module can cache the response content process for subsequent requests, directly return the cached content to improve the response capability ).

8, postmaprequesthandler

ASP. NET has different httphandler processes for accessing different resource types. For each request, ASP. NET selects the corresponding httphandler type through the extension. After successful matching, this implementation is triggered.

9-10, acquirerequeststate, postacquirerequeststate

ASP. NET triggers these two events successively, so that the status management module obtains the corresponding status based on the current request, such as sessionstate.

11-12, prerequesthandlerexecute, postrequesthandlerexecute

ASP. NET finally processes requests through httphandler corresponding to the request resource type. Before and after implementing httphandler, these two implementations are successively triggered.

13-14, releaserequeststate, postreleaserequeststate

ASP. NET triggers these two events successively, so that the Status Management Module releases the corresponding status based on the current request.

15-16, updaterequestcache, postupdaterequestcache

ASP. NET has successively triggered these two events so that the cache module saves the httphandler requests to the output cache.

17-18, logrequest, postlogrequest

ASP. NET successively triggers these two events as logs of the current request process

19, endrequest

After the entire request is processed, the endrequest event is triggered.

Create an object of the requested page class in 8th events and convert it to an ihttphandler Interface Class Object .,

In the 9-10 event, the sessionid sent by the browser is accepted, and the corresponding session object is found in the session pool of the server based on this value, and assigned the session attribute to the page class object.

Execute the processrequest method of the page class between 11th and 12 events.

Below is the State Persistence: Session, Cookie viewstate. After finishing the writing, keep the status and carefully check what has been done in the events from 11 to 12.

The cookie is saved on the browser side. The cookie has two statuses: one is saved in the memory of the client computer, the cookie created when the page is accessed (that is, the cookie with no expiration time set ). Another method is to set a cookie with a positive expiration time, which is saved in the cookie folder corresponding to the browser. Before setting a cookie, the browser sends a request to the server. The server needs to set a cookie for the browser, so it sends a cookie to the browser and stores it in the memory or hard disk of the client. After a cookie is set, each request page sends the cookie to the server.

 

The server reads cookies to learn your related information and then performs corresponding operations.

Session is implemented based on cookies. The difference is that sessions are stored on the server.

For example, when we log on to the server, the server will set a session for us to be saved on the server side, and a sessionid will be generated and sent to the browser side. the browser will store this sessionid, when you request another page, the browser sends the sessionid to the server ., The server automatically finds the session object corresponding to the sessionid in the session pool of the server based on the sessionid sent from the browser and assigns the session attribute to the current page object.

If the cookie is disabled in the browser, the server can also pass the sessionid between the browser and the server by saving the sessionid in the URL. (You Need To set cookieless = "autodetect" for the sessionstate node in the configuration file ")

<Sessionstate cookieless = "autodetect"> </sessionstate>

Viewstate: The viewstate attribute of the page actually obtains the value of a hidden field submitted by the browser named _ viewstate.

To use viewstate, you must have a runat = "server" form.

When the page-class object executes the processrequest method, it first creates a control tree, and then executes the loadstate method to encode the _ viewstate in the request message in base64 format and then serialize it, finally, it is restored to a set, and then the key-value pairs in the viewstate added by the programmer are restored to the viewstate attribute of the page object, and then page_load is executed ., Then run savestate to save the data to the viewstate attribute.

 

The processrequest method of the page class object is executed between 11th and 12.

1. Call the processrequest method of the parent class. In this method, the parent class calls the frameworkinitialize () method of the parent class, but because it is overwritten by the page class, therefore, the frameworkinitialize () method of the current page class is executed. The _ buildcontroltree is called in the middle.

2. Create a control tree

Front-end page class.

The foreground page class is inherited from the background page class.

 

 

The background page class is inherited from the page

 

 

 

 

 

 

Templatecontrol inherits from control

 

 

 

In the coltrol class

 

 

There is a control set in the middle.

 

 

That is to say, our front-end page class contains a set of controls based on the inheritance relationship.

Then go to the front-end page class.

, A processrequest method is found at the end.

 

 

, Go in.

 

 

The bucket controltree begins to build a control tree.

 

 

Foreground class inherits background class

Litralcontrol contains HTMLCode.

Htmlhead-htmltitle ......

 

 

 

 

 

The _ buildcontroltree method encapsulates all the front-end code and encapsulates it into different control objects according to different labels.

 

 

 

3. Execute the page Lifecycle

1. page_init ();
2. Load viewstate and PostBack data;
3. page_load ();
4. Handle control events;
5. page_prerender ();
6. page_render ();
7. Unload event;
8. dispose method called;

Phase

Description

Page request

Page requests occur before the page lifecycle starts. ASP. net will determine whether to analyze and compile the page (to start the page lifecycle), or whether it can send the cached version of the page to respond without running the page.

Start

In the initial stage, page properties such as request and response are set. In this phase, the page also determines whether the request is a send-back request or a new request, and sets the ispostback attribute. In addition, the uiculture attribute of the page is set during the start phase.

Page Initialization

During page initialization, you can use controls on the page and set the uniqueid attribute for each control. In addition, any topic will be applied to the page. If the current request is a send-back request, the send-back data has not been loaded, and the control property value has not been restored to the value in the view State.

Load

During the loading, if the current request is a send-back request, the control properties will be loaded using information restored from the view status and control status.

Verify

During verification, the validate method for all validators controls is called, which sets the isvalid attribute for each validators control and page.

Event Processing

If the request is a send-back request, all event handlers are called.

Rendering

The view status is saved for the page and all controls before rendering. In the rendering phase, the page calls the render method for each control. It provides a text writer to write the control output toResponseAttribute in outputstream.

Uninstall

After the page is completely rendered and sent to the client, the call is uninstalled after the page is discarded. In this case, the page properties (suchResponseAndRequest) And perform cleanup.

 

4. Call the render method of the page class to generate HTML code.

 

The above seems messy. Below is a summary.

1. the browser requests a dynamic page. IIS finds itself unable to process the request and transfers the request to the ing table.

2. The ASPX page in the ing program corresponds to aspnet_isapi.dll, so the request is forwarded to ISAPI

3. The request message is sent to httpruntime through isapiruntime.

4. Create the isapiworkerrequest object WR in httpruntime and encapsulate the request message in WR. Create an httpcontext context object using a series of methods, including httprequest and httpresponse.

5. Then, create an httpapplication object through httpapplicationfactory (this object is responsible for truly processing the creation and execution of the page object, first in the httpapplication pool to see that there is no such object, there is no new one)

6. Call 19 standard processing events in the httpapplication request pipeline.

Create the requested page Class Object in 8th events

In the 9-10 event, accept the sessionid sent by the browser and find the corresponding session object in the session pool of the server based on this value, and assign it to the session attribute of the page class object.

The processrequest method of the 11-12 event execution page class. Create a control tree, execute the page lifecycle, call the render method of all control objects in the page class, and generate HTML code

7. Return the HTML code to the browser.

 

Related Article

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.