Introduction
I have read a lot of Asp. Net books and found that most authors have explained Asp. Net at a relatively high level. They patiently and meticulously tell you how to drag and drop controls step by step, set control properties, and write CodeBehind code to implement a specific function.
This is actually an answer"How to do"But did not answer"Why?".
Although I highly recommend Mr. Shi jianghua's "Asp of temple sacrifices. net development details, but when I looked at its Role and user (Member) explanations, I decided to skip and read the subsequent chapters directly. I found that he also followed the big stream, and the explanation of this part stays at the "how to do it" level. I believe that a cool man like Mr. Shi cannot not understand the underlying operating principles, just because the book is already very thick.
When you develop a program based on the content described in "how to do", you are still a programmer for your users; however, for Microsoft developers who have implemented the MembershipProvider and RoleProvider abstract classes, you have become one of their users.
NOTE:I have no objection to the fact that some authors only explain "How to do it" or that you only learn "How to do it". This also has the advantage of rapid development. I only recommend that you have a better understanding of some problems.
I hope that through this series of articles, you can better understand the operating principles and practices of Asp. Net.
HttpRequest Processing Process Overview
"Why do I enter www.tracefact.net in the address bar to see Zhang Ziyang's personal space ?", It is similar to thinking: "Why did Apple go to the ground instead of the sky ?". For ordinary visitors, this is taken for granted as if the sun rises in the east and falls in the west every day. For many programmers, it is the responsibility of system administrators or network administrators to consider this as irrelevant. After all, IIS is a component of Windows and is not an integral part of Asp. Net. In fact, IIS and. Net Framework have already done a lot of behind-the-scenes work within a tenth of a second.
You may feel that it is irrelevant to understand how these behind-the-scenes jobs work. As a programmer, you only need to ensure that the developed programs can run efficiently. However, during development, you often need to use classes such as HttpContext. At this time, have you ever thought about the composition of these classes and how they are created? You may simply answer: HttpContext represents a context of the current request. But you know IIS, Framework, Asp. net is how to work together to process each Http request, how to distinguish different requests, IIS, Framework, Asp. net data flow between the three?
To answer these questions, you must first understand how IIS processes page requests. This is also the basis for understanding Form and Windows authentication modes.
HttpWhen the request arrived at the server
When the server receives an Http request, IIS first needs to determine how to process the request (NOTE:Is it true that the server processes A. htm page and A. aspx page ). So what does IIS do? -According to the file suffix.
The server obtains the requested page (NOTE:It can also be an extension of a file, such as jimmy.jpg). Then, the server will find an application that can process such extensions. If IIS cannot find an application that can process such files, this file is not protected by the server (NOTE:A protected example is the file in App_Code, and an unprotected example is your js script). Then IIS will directly return the file to the client.
Applications that can process various extensions, usually called ISAPI applications (NOTE:Internet Server Application Programe Interface, Internet Server Application Interface ). Although this ISAPI sounds very elegant, it is also an "application", but you can see it by looking at its full name:It is actually only an interface that acts as a proxy. Its main task is to map the requested page.(File)And the actual handler corresponding to the suffix.
Let's take a closer look at the ISAPI and see what it looks like. Please follow the steps below:
- Open IIS.
- Select a site at will, right-click the site, and select "properties ".
- Select the "home directory" tab.
- Select "configuration ".
You should see the following picture:
Figure1.Application configuration
Clearly, we can see that all the file types that can be processed by IIS or that is called the proxy service provided by ISAPI and their corresponding actual background processing programs are clearly listed here.
Find the. aspx application handler and click "edit". The following figure is displayed:
Figure2.Edit. AspxFile Handler
All the way here, we can see that all. aspx files are actually processed by the aspnet_isapi.dll program. after the request for the aspx page is submitted to aspnet_isapi.dll, it no longer cares about how the request is processed subsequently. Now we should know:Asp. NetServer only(IIS)Is an integral partISAPIExtension.
Note the following two points:
- After you change "limit to", you can restrict access to pages (Files) in a specific way.
- "Check whether a file exists" is a key option for URL address ing. I will detail it later.
Understand the host environment (Hosting)
In essence, Asp. Net is mainly composed of a series of classes whose main purpose is to convert Http requests into responses to clients. The HttpRuntime class is a main entry of Asp. Net. It has a method called ProcessRequest. This method uses an HttpWorkerRequest class as a parameter. The HttpRuntime class contains almost all information about a single Http request: the requested file, server variables, QueryString, and Http header information. Asp. Net uses this information to load and run the correct file and convert the request to the output stream. Generally, it is an HTML page.
NOTE:In other words, it can also be an image.
When the content of the Web. config file changes or the. aspx file changes, in order to uninstall the application running in the same process (NOTE:To reload), Http requests are divided into isolated application domains.
NOTE:You may have heard of the application domain before, but you don't know what's going on. The application domain is AppDomain.
For IIS, it depends on a built-in driver called HTTP. SYS to listen to HTTP requests from outside. When the operating system is started, IIS first registers its own virtual path in HTTP. SYS.
NOTE:Actually, it is equivalent to telling HTTP. SYS which URLs are accessible and which are inaccessible. For example, why do you encounter a 404 error when accessing a non-existent file? It is determined in this step.
If the request is an accessible URL, HTTP. SYS will send the request to the IIS worker process.
NOTE:IIS6.0 is called w3wp.exe, and IIS5.0 is called aspnet_wp.exe.
Each worker process has an identity and a series of optional performance parameters.
NOTE:Optional performance parameters, such as the recycle mechanism settings and timeout settings.
The next step is what we described in the previous chapter.ISAPI.
NOTE:This part of the content is highly correlated. In order to make everyone better understand it, I decided to put the ISAPI in front of it. It may be adjusted when the full series is completed.
In addition to the corresponding handler of the ing file, ISAPI also needs to do some other work:
- Obtain the current Httq request information from HTTP. SYS and save the information to the HttpWorkerRequest class.
- Load HttpRuntime in the AppDomain of the mutually isolated application domain.
- Call the ProcessRequest method of HttpRuntime.
The next step is the work that programmers usually write. Then, IIS receives the returned data stream and returns it to HTTP. SYS, and finally, HTTP. SYS then returns the data to the client browser.
OK. Now you can see Zhang Ziyang's space homepage.
Figure3. Asp. NetHost environment
Understand MPs Queue (Pipeline)
In the previous two chapters, we discussed at a relatively low level what IIS and Framework did in a second from sending an Http request to seeing the browser output. However, we ignore the details of how the Code Compiled by the programmer is connected in this process. Let's take a look at this issue in this chapter.
When an Http request enters Asp. net Runtime, its pipeline consists of a hosting module (NOTE: Managed Modules) and a processing program (NOTE: Handlers), and the pipeline is used to process this Http request.
Figure4.UnderstandingHttpMPs queue
Let's take a look at how the data in this figure flows by number.
1. HttpRuntime transfers an Http request to HttpApplication. HttpApplication represents a Web application created by a programmer. HttpApplication creates an HttpContext object for this Http request. These objects contain many other objects related to this request, including HttpRequest, HttpResponse, and HttpSessionState. These objects can be accessed through the Page class or Context class in the program. ,
2. Next, the Http request uses a series of modules that have full control over the Http request.TheseModuleYou can do things before performing a specific job..
3. After the Http request passes through all modules, it will be processed by HttpHandler. In this step, perform some actual operations, usually the business logic completed on the. aspx page. You may not understand this process when creating the. aspx Page. However, you must know that the. aspx Page inherits from the Page class. Let's take a look at the Page class signature:
Public class Page: TemplateControl, IHttpHandler {
// Code omitted
}
As you can see, the Page class implements the IHttpHandler interface, and HttpHandler is also the lowest layer for Http request processing.
4. After HttpHandler completes processing, the Http request returns to the Module again,In this caseModuleYou can do things after a job is completed.
NOTE:Pay attention to the words marked in red, and think back: Is there many paired events such as Inserting and Inserted in Asp. Net? In fact, Asp. Net can divide an Insert operation into two parts, and then intercept the background principles of the event separately.
If we focus only on Http requests, HttpHandler, and HttpModule without considering HttpContext and HttpApplication, figure 4 can be simplified as follows:
Figure5. HttpRequest inHttpHandlerAndHttpModuleFlow Direction in
Summary
In this article, I will first outline the topics that will be discussed in this series of articles. Then, I raised a question for some programmers: learning and using Asp. Net at a relatively high level.
Later, I took an example to access my personal space home page and introduced three things described in this article:
- What does IIS do when an Http request arrives at IIS.
- The host environment of the Http request.
- Http pipeline.
I hope this article will help you.