asp.net request input to the output of the whole process and HttpHandler and Httpmoduler Detailed introduction _ Practical skills

Source: Internet
Author: User
Tags httpcontext

Recently I read a few stories about HttpHandler and Httpmoduler, and in general, that article from Fish Li, but he is Daniel, his writing is too technical, for a soldier like me,

To fully understand the estimated need to see several times. Although the underlying operation was not fully understood, I also figured out what process a request has experienced from entering IIS to the final output. To tell the truth, originally I thought. NET class of the child

Classes are designed by the designers themselves and do not take into account whether the real programmer can fully master it. Knowing the underlying operation, I found that my view was so ignorant of each one. NET is a kind of corresponding reality

Like, for example, routes in MVC3 include Routedata and HttpContext, and why do they have to be included? Only by understanding the triggering process of IIS can we really understand it.

The preface is complete, so let's share my understanding of the bottom of the IIS. The technology is not perfect, can only use vernacular to say.

The above picture illustrates the process under IIS 6.

Since we are now using MVC3, I am going to describe a request from beginning to extinction in accordance with the life cycle in MVC3.

Request Phase :

The user enters the Localhost/home/index address through the browser, and the browser sends a request to the server for IIS to process the request. In fact, there is a system file in the operating system called HTTP.sys

File, which is used to monitor the arrival of requests, which means that the first person to receive a request from a user is HTTP.sys, a system file that runs in the kernel mode of the operating system, and therefore runs at a speed

Faster.

After the HTTP.sys file has received the request (note: This is one of my misconceptions, I used to think that the request would go directly to IIS), and it will pass to the second host IIS, the operating system group that is really used to process the request

Thing After IIS receives a user request, the corresponding application is first selected by the mapping file and then by the Aspnet_iisapi.dll (IIS extension) based on the file name extension. That's a bit awkward, straightforward.

Is that the IIS extensions select the handlers that are configured in IIS based on the file name extension (. aspx, and so on) that is passed in. There's a problem here, there's no extension in MVC, so how does a program match? Actually, this

There are two ways to deal with problems :

1. Spoofing IIS by adding a virtual extension to the routing table

2. It is by not selecting a confirmation file in the IIS configuration file that IIS is being processed based on a file path that does not have a file name extension

Now where does this request go? To the IIS extensions here, the next step is to get into the. NET Framework, let the. NET Framework handle the request. But there are some steps to be processed in the middle. You should remember that on the Web

There are a lot of events in the form, Page_Load, Page_render, and so on, and the sequence of these events is sequential, not confusing? So. How does the NET Framework guarantee the sequential execution of these events? This is the present

The first protagonist of the day HttpModule. We can call it a filter for HTTP requests because it will not have any output, it will be executed in any request. One exception, of course, is the static file or

Other request files that are not configured to allow IIS to extend the. NET Framework processing because they enter IIS, IIS finds the corresponding file and outputs it to the browser.

HttpModule the specific use of Daniel are very clear, I will briefly describe the ignorance of Daniel's points of knowledge. Now that HttpModule is a filter, then we can httpmodule in any one

To terminate the execution of the current request, the implementation of identity authentication, please check the file access rights and other operations. We can customize HttpModule extensions, just let our own defined classes implement the IHttpModule interface,

There is an init (HttpApplication app) method in the IHttpModule interface, which is the portal to our custom extension module, where we can define our own processing operations.

Init This method takes a HttpApplication type of argument, HttpApplication is defined in MSDN as the common methods, properties, and events that define all application objects in the ASP.net application

Thing This class is the base class for the application defined by the user in the Global.asax file. Instances of the HttpApplication class are created in the ASP.net infrastructure, not created directly by the user.

An instance of the HttpApplication class is used to process multiple requests during its lifetime, but it can only process one request at a time. This allows member variables to be used to store data for each request.

See the definition of this class we have no idea of the application pool, and in IIS we create a new application that creates a corresponding application pool, but what is stored in the application pool? It's supposed to be.

These HttpApplication objects. Each request has a corresponding HttpApplication object that is responsible for its execution and contains all the parameter values required by the request in the HttpApplication object. For example

Response, Request, cache and so on. NET commonly used objects, even we can get through this variable to all the module extensions defined in Web.config. HttpApplication will be with all the requests.

Execution process.

Now a problem comes up, this module extension needs to pass a HttpApplication object as a parameter, so who is the parameter of the method created? We should often use a class

HttpRuntime, according to this literal meaning, we can also think that this is the HTTP runtime represented, yes, after IIS is ready to request the data will be called through HttpRuntime

HttpApplicationFactory a Create () method to get a HttpApplication object and then pass the parameter value to the object, which is passed to the module extension.

Now that the request has been filtered by the module extension, it's time to go to the place where it's really handled, HttpHandler, mention it, if we're a little unfamiliar, then we must have used it. NET, the general handler in

We can see that the generic handler is a ashx file that inherits from the IHttpHandler interface for ProcessRequest processing. In fact, our HttpHandler is the codebehind of ashx documents.

File. As long as we implement the method in the IHttpHandler interface, we define a handler extension.

HttpHandler is the role of the processor, not the filter, so handler will have output. If you want to use the session in handler, you will inherit Irequiredsessionstate

interface, or a ireadonlysessionstate interface, so that we don't get an error when we operate the session. In handler we can do whatever we want, such as creating a picture

Watermark, anti-theft chain even the output of the file compression and coding, etc. can be achieved.

Like our web service and general handlers, which are essentially handler a high-level implementation, are handler extension operations.

Because we're talking about MVC, we have to consider routing route, which in fact route is a separate component in MVC, and it plays a very important role in our entire request. When IIS expands through IIS

The show chooses the appropriate handler to process the request, when the route appears, routing will analyze the controllername and actionname of the path according to the routing configuration, then the corresponding parameter value,

The parameters are then stored in Routedata, Routetable.routes is a routing set, and the Routedata and HttpContext contexts form the object of another class, RequestContext, I

In MVC programming, we often use some of the data in this object. NET Framework matches the Controller and action in the program based on the value of the RequestContext object, and then calls the

Controllerdescriptor executes the controller, generates the controller object, and then executes the specific action through the Actioninvoke method.

When the action completes, and the corresponding view is returned, the entire request is in the. NET Framework, even if the process ends. The output is also passed at the end of the module extension before the output returns to the user's browser

, the output reaches IIS, and the last IIS responds to the user's browser via http.sys, and the user can see the output.

Because a request is expanded by a module two times from entry to display in the browser, that's why we can define a start event in the Web form, and then there's a reason to complete the event.

To sum up, a user initiated a request through the http.sys-->iis-->aspnet_iisapi.dll--> corresponding handler-->module--->handler-->module--- >IIS-->http.sys--> User Browser.

Of course, the order of the request is not particularly accurate, because a lot of details are omitted, but these are the features in the big sense. You may have a question, when is the ASPX file not implemented? Actually, this

A question I have thought before, ASPX is processed after module, but after handler or before? Today finally got the answer, in fact a separate ASPX file is a handler, each

The aspx file is compiled into a class that inherits from the page, but where does the page inherit from?

Copy Code code as follows:

public class Page:templatecontrol, IHttpHandler

We can see that the page inherits from the IHttpHandler interface, which verifies that the page class execution is triggered when handler executes.

A small HTTP request will let us have so much knowledge to master, we as the programmer for this request model should be very familiar. But as a programmer who developed the. Net drag control, I would kindly remind

If you can not control, we do not use, with JS, CSS to replace it, after all, HTML is the basis. At the time of the MVC era, embrace the new technology.

I'm a soldier, I don't have a lot of say, so I just follow the idea of a pawn to analyze Daniel's technique.

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.