) Asp. Net Request Processing Mechanism

Source: Internet
Author: User

Original article: http://www.cnblogs.com/cilence/archive/2012/05/28/2520712.html

 

We all know that the Web request response is based on the Http protocol, so we can understand that a Web request and response process is actually a process of sending an Http request and receiving an Http response. The client sends an Http request to the server. The server receives the request, generates a response packet, and sends the response packet back to the client. This completes a Web interaction between the client and the server. Asp. Net is an AOP framework that runs on CLR managed code and processes Web requests and responds to Web requests from the past. It is an engine used to process Web requests. It is not just a commonly used WebForm, WebService, IHttpHandler, but also a lot... let's take a look, when we enter http://www.cnblogs.com Click the Enter key when accessing the blog site. At this time, a Request is generated from the browser and sent http://www.cnblgos.com When the request arrives at the web server, the HTTP. SYS component in the Windows Kernel of the web server will capture the request. When the HTTP. SYS component analyzes that this is an Http Request to be sent to the IIS server for processing, the HTTP. SYS component will send the Request to the IIS server for processing. The IIS server analyzes the context-type of the Request and then matches it from the handler ing table. When the context-type of the Request can be matched in the handler ing table, the IIS server then submits the request to the corresponding program in the ing table for processing. When IIS finds that there are no matching items in the handler ing table (when there are no matching items, it usually requests "static files "), directly download the file in the corresponding path of the Request. Such as. jpg, HTML, and xml.css files. When the server processes dynamic files such as aspx. ashx and so on, the IIS server sends the Request to the aspnet_isapi.dll file for processing. Aspnet_isapi.dll is a very underlying and unmanaged win32API. It is very simple and efficient, and has been optimized by Microsoft. It is used to process the underlying commands and function callback, and provides the application-level service functions and interfaces for high-level programs. When the aspnet_isapi.dll receives the Request, the aspnet_isapi.dll will be transferred to the Web server. net Framework, and finally load the CLR runtime environment, create an ISAPIRuntime object, and then call the ProcessRequest () method of the ISAPIRuntime object. ISAPIRuntime. after the ProcessRequest () method is called, the original Request information of the Request is encapsulated into the HttpWorkRequest class. Because the Request packets encapsulated by the HttpWorkRequest class are very primitive and complex, so Microsoft did not publish it. Run the StartProcessing () method to create the HttpRuntime object and call its static method ProcessRequest (). After the HttpRuntime object calls its static method ProcessRequest (), our Web requests start slowly entering the application layer level, why? ProcessRequest () does a lot of work. You can use the Reflector tool to view details, but it is roughly divided into four parts: 1. create a new HttpContext instance for the request (this object is our common HttpContext context object), and encapsulate the most primitive request packets in HttpWorkRequest into the HttpRequest object of the HttpContext object. 2. Use HttpApplicationFactory to obtain a specific HttpApplication instance. 3. Call HttpApplication. Init () to create an event request pipeline. 4. The Init () method triggers the HttpApplication. ResumeProcessing () method to start executing the Asp. Net event request pipeline. As mentioned above, Asp. Net is an Aop framework, while Asp. Net event request pipeline is one point. Asp.. Net event request pipeline is a list of events that Microsoft provides to help programmers process Web requests. These events are executed in sequence, we can modify the Web request execution logic by registering or removing methods we write on these events. In the eighth event, the requested page object is created and converted to the IHttpHandler interface. Between the ninth event and the second event, the SessionId sent by the browser is received. First, the IHttpHandler interface will be converted to the IRequiresSessionState interface. If the conversion is successful, Asp. net searches for the corresponding Session object based on the SessionId in the Session pool of the server, and assigns the Session object to the Session attribute of the HttpContext object. If the API fails to convert to IRequiresSessionState, the Session is not loaded. Between 11th and 12th events, the ProcessRequest method of the page object created in the eighth event is called. When we directly use the *. ashx page, a FrameworkInitialize () is called directly, and a response packet is generated and sent back to the client. When we use the *. aspx Page, it inherits from the Page class, while the Page class implements the IHttpHandler interface. Because the Page class implements the IHttpHandler interface, the FrameworkInitialize () method is called in the ProcessRequest method. In the FrameworkInitialize () method, Asp is created. net page Control tree (Create html tree), in which the ProcessRequestMain method is called, and the entire Asp is executed in this method.. Net page lifecycle. The request is sent through the event pipeline, and some column events are triggered. We can see these events in the Global. asax Global configuration file. However, because this is an event assigned by the program, it may not be what we want. If we want to create an HttpApplication event pipeline that can be reused to process Web requests, we want to reuse the code or develop it into a plug-in form. Then we can use IHttpModules. IHttpModules is easy to configure. You only need to configure it in Web. config. The specific HttpModules only needs to implement the IHttpModules interface and register its own method. = 1.0?> >>>== Cnblogs. cnblogsHttpModules/>>>>: IHttpModule MyBeginRequest (sender, EventArgs e) HttpApplication;); Asp. net is an Aop framework, while Asp. net event request pipeline embodies the idea of Aop, and now we want to talk about Asp. net page lifecycle also reflects the idea of Aop. The essence of Asp. Net page lifecycle is some column events that Microsoft provides to our programmers to modify the Page Control tree code. We can modify the control Tree Code by implementing the page lifecycle event method. When ASP. net, the Render method is called when the registered lifecycle event method is executed. The Render method requires passing in a TextWriter text writer object and traversing all control trees, call the Render method of each control. Therefore, the Html strings generated after each control calls the Render method are written to the TextWriter object in sequence. ASP. Net encapsulates the Html string in TextWriter into a Response Message and sends it back to the client. Here, I will introduce the entire process of processing requests in ASP. Net. However, there is a lot of underlying information that I have not carefully observed, so many details may still not be noticed. This article may still have many errors. I hope that the brothers and sisters in the blog garden can correct the errors. Thank you.

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.