The previous day I learned the HTTP protocol, simple socket programming, and started to enter the formal Asp.net learning yesterday. The first-stage learning is roughly divided into five stages:
- General ProcessingProgram, HTML controls, and. net internal objects. Objective: To understand the operating principles of ASP. NET and have a preliminary understanding of several Asp.net core objects.
- Webform aspx: Control tree, page number generation, etc.
- Ajax: Understand the Ajax Implementation Mechanism
- Webform Server Control: Nothing to say
- Asp.net high-level content: Cache/dashboard page/error page/static page/website deployment/Page lifecycle/httpmoudle/URL rewriting, etc.
I started to study at the 1st stage of the entry stage yesterday. I learned the entire preliminary process because I had a web server (simulated the communication between the browser and the server in my previous blog) the day before yesterday, so learning the general processing program is not a problem. let's take a look at a small demo: first create a login HTML page and a general processing program:
<Form action = "login. ashx" method = "get">
<Input type = "text" name = "username" value = ""/>
<Input type = "text" name = "password" value = ""/>
<Input type = "Submit" name = "Submit" value = "Submit"/>
</Form>
When we enter the user name and password and submit it to the General handler for help:
Public void processrequest (httpcontext context)
{
// Specify the output format
Context. response. contenttype = "text/plain ";
Context. response. Write ("Hello World ");
Context. response. Write (context. Request. querystring ["name"] + "Welcome ....");
}
Of course, it is very inconvenient for a program to process a large amount of HTML output, and HTML needs to be spliced.CodeIt is inconvenient to use. I will not demonstrate it here. Of course, it is very useful to process files and images, so that webform appears, the front-end page can be regarded as a template (anyone who has used codesmith knows the template and can write C # code). The front-end page inherits the background, therefore, some background page items can be added to the foreground. After the background processing is complete, the foreground changes accordingly, so you do not need to enter a large amount of HTML code, for the moment, I regard the webform front-end page as a template. OK. Do not say webform.
When we enter the login name and password and submit the button, what does the browser help us and how does the server receive the data? We seem very simple. The browser encapsulates the request into an HTTP request, and then sends the request to the server through socket, and the service is handed over to the general processing program for processing. In fact, this process has gone through a lot of processing, next let's take a look at the detailed processing process.
Client-----http.sys--iis-----w3svcservice -----inetinfo.exe ---asp.net _ ISAPI ---- w3wp. EXE ---- isapiruntime ------ httpruntime ----- httpapplication
when the client sends a request to the Web server through a socket, it first reaches a kernel module HTTP. sys, which is responsible for parsing the protocol and port of the current request. In addition, it distributes the current request to the HTTP request processing program and then arrives at the Inetinfo of the w3svc service on the familiar IIS server. EXE will analyze the metadata in the process. If it is a static page, it will be processed directly. If it is ashx or aspx, it will be handed over to Asp.net _ ISAPI, this is a com program written in C ++. It is responsible for starting net runtime and handing HTTP requests to Asp.net runtime... this dll is very important and can be regarded as a bridge. after arriving at isapiruntime, The processreuquest method is called. The PR method is called for short. The request message of IIS is obtained through a ECB handle, and an httpworkrequest object is created, it encapsulates the request we send to-and then to httpruntime, it also has the PR method, the httpworkrequest is then encapsulated to support httpcontext, which includes httprequest and httpresponse, httpapplication is reflected by the factory. There is an httpapplication application pool, and then there are 23 events in httpapplication, 19 of which are public. We created our handerl instance in 8th events, call the pr method of the handerl instance between events 11 and 12. such a general processing program will almost end. After the event ends, it will be returned to the client in the original path. I feel that if we want to see the source code and other httpruntime, it is an entry. we can use the decompilation tool to check how the program is written.
After talking about this, what can we learn? What is the use of this? I don't know the Asp.net operating mechanism. If something goes wrong, you don't know where it is, the key is that we can also learn the idea that we can expand the 23 events in the request pipeline. Through httpmodule and so on, the httpapplication itself registers these events and can regard httpmodule as a filter, we can learn this plug-in mechanism. If we have the ability, we can use it in future projects. the following figure shows an approximate process. This figure is not very detailed. We recommend you read a blog and write a very good notebook ~