I recently read several articles about httpHandler and httpmod.pdf. In general, it is still the article by Fish li, but he is a great guy. The article he wrote is too technical, for a soldier like me,
To fully understand it, you need to read it several times. Although I do not fully understand the underlying operations, I also understand the process of a request from entering IIS to the final output. To be honest, I thought that the sub-of the. Net class
Classes are designed by the designers themselves, without considering whether Real programmers can fully master them. After understanding the underlying operations, I found that my opinion is so ignorant that every. Net class corresponds to one of
For example, routes in Mvc3 include RouteData and HttpContext? Only by understanding the triggering process of iis will we truly understand it.
After the introduction, I will share my understanding of the underlying IIS. The technology is not hard. It can only be used in vernacular.
The preceding figure shows the processing process under IIS 6.
Because MVC3 is used now, I will describe the whole process from the beginning to the end of a request according to the lifecycle in MVc3.
Request Phase:
Enter the address of localhost/home/index in the browser. The browser sends a request to the IIS server to process the request. In fact, there is a system file in the operating system called http. sys.
File, which is used to monitor whether a request arrives. That is to say, the first receptionist of a user's request is http. sys, which is a system file and runs in the kernel mode of the operating system.
Faster.
In http. after the sys File receives the request (Note: This is a misunderstanding of mine, I used to think that the request will directly go to IIS), it will be passed to the second receptionist IIS, real OS group used to process requests
. After IIS receives a user request, it first uses the ing file and then selects the corresponding application based on the file extension by aspnet_iisapi.dll (IIS extension. This means something straightforward.
That is, the IIS extension selects the handler configured in IIS based on the input file extension (. aspx, etc. There is a problem here. There is no extension in Mvc, so how does the program match? Actually, this
There are two solutions to the problem.:
1. spoofing IIS by adding a virtual extension to the routing table
2. Check whether the file exists in the IIS configuration file, so that IIS can process the file path without the file extension.
Where is this request? In IIS extension, the next step is to go to the. Net Framework and let the. Net Framework handle requests. However, some steps will be taken between them. You should remember
There are many events in form, such as Page_load and Page_Render. The execution sequence of these events is sequential and will not be chaotic? So how does the. Net Framework ensure the sequential execution of these events? This is today
The first main character of day HttpModule. We can call it an http request filter because it does not have any output and will be executed in any request. Of course, there is an exception, that is, static files or
Other request files that are not configured to allow the IIS extension to be processed by the. Net Framework. Because they enter IIS, IIS will find the corresponding files and then output them to the browser.
The specific use of HttpModule is clearly explained. I will briefly describe the knowledge points ignored by the experts. Since HttpModule is a filter, we can
Terminate the execution of the current request, perform identity authentication, and check the file access permissions. We can customize the HTTP module extension, just let our own classes implement the IHttpModule interface,
There is an Init (HttpApplication app) method in the IHttpModule interface. This is the entry to the custom extended Module, where we can define our own processing operations.
The Init method will accept a parameter of the HttpApplication type. The definition of HttpApplication in MSDN is to define the methods, attributes, and things common to all application objects in ASP. NET applications.
. This class is the base class of the application defined in the global. asax file.HttpApplicationClass instances are created in the ASP. NET infrastructure, rather than directly created by users.
HttpApplicationAn instance of the class is used to process multiple requests within its lifetime, but it can only process one request at a time. In this way, member variables can be used to store data for each request.
When we see the definition of this class, do we think of the concept of application pool? When we create an application in IIS, we will create a corresponding application pool, what is actually stored in the application pool? It should be
These HttpApplication objects. Each request has a corresponding HttpApplication object for full execution. The httpApplication object contains all the parameter values required by the request. For example
Response, Request, Cache, and other common. Net objects. We can even use this variable to obtain all the Module extensions defined in web. config. HttpApplication will be accompanied by all the requests
Execution Process.
Now again, the Module extension needs to pass an HttpApplication object as the parameter. Who created the parameter of this method? We should often use a class
HttpRuntime, according to the literal meaning, we can also think of this as the Http runtime, yes, after IIS prepares the request data, it will be called through HttpRuntime
An Create () method of HttpApplicationFactory to obtain an HttpApplication object, pass the parameter value to this object, and then the object will be passed to the Module extension.
Now that the request has passed the Module extension filtering, it is about to go to the place where it is actually processed. HttpHandler mentioned it. If we are a little unfamiliar, we must have used it. net,
We can see that the general processing program is an ashx file, which inherits from the IHttpHandler interface for ProcessRequest processing. In fact, our HttpHandler is the codeBehind of the ashx file.
File. As long as we implement the methods in the IHttpHandler interface, we define a Handler extension.
HttpHandler is used as the Handler role, not as a filter, so Handler will have output results. If you want to use Session in Handler, you must inherit the IRequiredSessionState
Interface, or an IReadOnlySessionState interface, so that we do not encounter errors when operating the Session. In Handler, we can perform any operations we want, such as generating images.
Watermarks, anti-leech protection, and even file output compression and encoding can all be implemented.
Like our Web Service and general processing programs, they are essentially Handler's high-level implementation methods, all of which are extended by Handler.
Because we are talking about MVC, we have to consider routing Route. In fact, Route is a separate component in Mvc, which occupies a very important position in our entire request. Expand through IIS in IIS
When an appropriate processing program is selected to process the request, the route will analyze the ControllerName and ActionName of the path based on the route configuration. However
These parameters will be stored in RouteData. RouteTable. Routes is a route set, and the RouteData and HttpContext context will form another class object, RequestContext, I
We often use some data in this object during MVC programming .. Net Framework matches the Controller and Action in the program based on the value of the RequestContext object, and then calls
ControllerDescriptor executes the Controller, generates the Controller object, and then executes the specific Action through the ActionInvoke method.
After the Action is executed and the corresponding view is returned, the processing of the entire request in the. Net Framework is complete. Before the output result is returned to the user's browser, the output result will go through the last part of the Module extension.
Finally, IIS responds to the user's browser through Http. sys, and the user can see the output result.
Because a request is extended by Module twice from entry to display in the browser, this is why we can define a start event in web form and then there is another reason to complete the event.
To sum up, a user initiates a request through http. sys --> IIS --> aspnet_iisapi.dll --> corresponding processing program --> Module ---> Handler --> Module ---> IIS --> http. sys --> user browser.
Of course, the order of this request is not very accurate, because many details are omitted, but these functions are mentioned in the big aspect. You may have a question: when is the aspx file not executed? In fact, this
I have previously thought that aspx is handled after the Module, but after handler? Today, I finally got the answer. In fact, a separate aspx file is a handler, and each
The aspx file is compiled into a class during compilation. This class inherits from Page, but where does Page inherit from?
Copy codeThe Code is as follows:
Public class Page: TemplateControl, IHttpHandler
We can see that the Page inherits from the IHttpHandler interface, which verifies that the Page class is triggered during Handler execution.
A small http request will give us so much knowledge to master. As a programmer, we should be very familiar with the request model. But as a programmer for. Net Drag Control Development, I kindly remind you that,
If you don't need controls, let's stop using them. Use js and css instead. After all, html is the foundation. When the Mvc era comes, embrace new technologies.
I am a small soldier and do not have much say, so I analyze the technology of the big bull based on the ideas of small soldiers.