Asp.net process a request

Source: Internet
Author: User

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 practice actually answers the question "How to do it", but does not answer the question "Why can this be done.

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 am not opposed to some authors who only explain "how to do" or "How to Do". This also has the advantage of fast 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.

Http Request Processing Process Overview
Think about "why do I enter www.2cto.com 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.

When an Http request arrives at the server
When the server receives an Http request, IIS first needs to determine how to process the request (NOTE: the server must process a .htm page and A. aspx page ). So what does IIS do? -According to the file suffix.

After the server obtains the suffix of the requested page (NOTE: it can also be a file, such as jimmy.jpg), it will find an application that can process such extensions on the server, if IIS cannot find an application that can process such files, and the file is not protected by the server (NOTE: A protected example is the file in App_Code, an unprotected example is your js script), so IIS will directly return this file to the client.

Applications that can process various extensions are generally 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 take a closer look at its full name to understand that it is actually only an interface and acts as a proxy, it maps the requested page (File) and the actual processing program 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:

Figure 1. 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:

Figure 2. Edit the. aspx file 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 that Asp. Net is only an integral part of Server (IIS) and is an ISAPI extension.

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.
Understanding 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 general, it can also be an image.

When the Web. the content of the config file is changed or. when the aspx file changes, in order to be able to uninstall applications running in the same process (NOTE: unmount is also to re-load), 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 Setting and timeout setting.

The next step is the ISAPI described in the previous chapter.

NOTE: This part of the content is highly correlated. In order to make everyone better understand it, I decided to put the ISAPI in the front. Maybe I will adjust it 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.

Figure 3. Asp. Net host environment

 

 

Understanding Pipelines)
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.

Figure 4. Understand the Http Pipeline

 

 

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. These modules can be used before a specific task is executed.

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. At this time, the Module can do some things after it has completed.

NOTE: Pay attention to the words marked in red. Then, let's take a look at whether there are 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:

Figure 5. Http request flow direction in HttpHandler and HttpModule

 

 

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.

 

From hi_dzj's column

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.