Taken from the Internet, some images are dead links and are deleted.
ASP. NET operating mechanism
1 HTTP Request
For example, let's take a look at what an HTTP request is. For a simple example, you can click a hyperlink "Microsoft programmer Test Question" on a web page ", if the link is a http://www.frontfree.net/view/article_840.html, the browser immediately sends an HTTP request to the server www.frontfree.net. however, this request includes not only the http://www.frontfree.net/view/article_840.html#string, but also the response. Of course, this is also required in the http2.0 specification, followed by some other information, such as the host name you requested, the passing router, and some information about the client, etc, therefore, an HTTP request contains a considerable amount of information, which is not as simple as we thought on the surface. now we know what an HTTP request is. next we will talk about ASP. net network application running mechanism.
2 ASP. Net Operating Principle Overview
For example, after an HTTP request is sent and received by the IIS server, IIS first loads the corresponding DLL file for the requested page type, then, the request is sent to the module that can process the request. in net, this module is called httphandler. Why can aspx files be processed by the server? That is because the default httphandler on the server is used to process aspx files, before IIS sends this request to a module that can process this request, it also needs to go through some httpmodules, which are default modules of the system, in addition, different httpmoduls must be processed before the HTTP request is sent to httphandler. This is like if we want to fly an international flight to a foreign country, before you actually get on the plane, the airport construction fee should be purchased, the identity should be verified through registration procedures, the luggage should be packed and checked, and the security inspection should be conducted. Now, the temperature and other inspections should be carried out, and a series of strict procedures should be adopted. What are the advantages of doing so? One is for some necessary processes, the other is for security, the third is to improve efficiency, and the fourth is for us to be able to control more links, that is, our control capability is enhanced. The above are all systems. How can we say that we can control HTTP requests? Can we establish our own httpmodule and httphandler? The answer is yes. But we should not rush to do things first. Let's take a deeper look at the operating mechanism.
3 ASP. NET operating mechanism
The figure above allows us to see more clearly how an HTTP request is processed by the server. At the same time, we can see that the request is in charge of all client inputs. The figure shows four possible routes for an HTTP request. When you access this page for the first time, the request first goes through the processing of httpmoduls and httphandler, and in the processing of httphandler, the server will forward you to the page you actually want to access, then, you can use ASP engine to find the class behind the page and instantiate it as a temporary object. In this process, a series of events are triggered, some of the events need to be processed by the method in the object. Then, the server will send the processed page to the response object and then send the page to the client by the response object. This is the first route. When you resubmit some information on this page and continue sending requests to the server, because the sessions between your servers have been established, the temporary object you are located has been created on the server, so you do not need to initialize the page. Therefore, the second route is based on httpmoduls and httphandler, and then directly interacts with the temporary object, then return. The third line is different from the second line. when processing a request, if ASP cache needs to be called, the temporary object will directly extract information from the ASP cache and return it. the fourth route is when you refresh the page, the server receives the request and finds that the request has been previously processed, the processing results are stored in the output cache managed by a default httpmodule. Then, we can extract information from the cache and return the results without re-processing them.
Image leeching
1. What is image leeching?
Let's analyze the general browsing phenomenon first. The most important point is that a complete page is not transmitted to the client all at once. if you are requesting a page with many images and other information, the first HTTP request is sent back the text of this page, then explain the text in the browser of the client and find that there are images in the text. Then, the browser of the client will send an HTTP request, after the request is processed, the image file will be sent to the client, and then the browser places the image back to the correct position on the page, A complete page may be displayed only after multiple HTTP requests are sent. based on this mechanism, a problem occurs, that is, leeching: If a website does not contain the information in the page, it can connect the image to other websites. in this way, websites without any resources use other website resources to present the resources to the viewers, which increases their access traffic, and most viewers will not easily find it. Obviously, it is unfair for the website that has been used resources.
2 solution
Now, Asp. the httphandler in net can solve this problem well. this is because by default, we only process dynamic web pages, such as ASP and Aspx. However, when an image file is requested, IIS directly extracts resources and sends them to the client, it seems a little blind.
Therefore, we need to create our own httphandler to process image files. For example, JPG files.
3. How can we create our own httphandler and register it in a web application?
(1) Build your own httphandler
Create a class that inherits the system. Web. ihttphandler interface. The system. Web. ihttphandler interface has only two members.
1. isreusable attribute. The returned value indicates whether other HTTP requests can use the instance that currently inherits the system. Web. ihttphander interface class.
2 processrequest (system. Web. httpcontext context) method, except for special HTTP requests that are required to be processed in user-defined processes.
The instance with the parameter system. Web. httpcontext class is loaded with all the information required by the HTTP protocol in an HTTP request. System. web. the httpcontext class contains a property request that allows the value of the HTTP request information sent from the client to be easily read; the property response encapsulates the information and operations to be returned to the client. Of course there are still many common attributes and methods, so we will not detail them here. We only use these two attributes here.
(2) register the custom httphandler in the Web Application
Add registration information to the Web. config network application configuration file.
<Httphandlers> <Add verb = "Path =" *. jpg "type =" Name of the Custom Handler class, network application name "/> </Httphandlers> |
4. Let's take a lookProcessrequest (system. Web. httpcontext context)The method is to process the HTTP request for image files.
Public void processrequest (system. Web. httpcontext context) { If (context. request. urlreferrer. host = "www.frontfree.net") // checks whether the image is a local reference. If yes, it returns the correct image to the client, the judgment here is the reference page information recorded in the HTTP request described above. { Context. response. expires = 0; // set the file expiration time in the client buffer to 0, that is, it expires immediately. Context. response. Clear (); // clear the output cache opened by the server for this session Context. response. contenttype = getcontenttype (context. Request. physicalpath); // obtain the file type Context. response. writefile (context. Request. physicalpath); // write the request file to the server end's output cache for this session Context. response. End (); // transmits the information in the output cache opened by the server for this session to the client. } Else // if it is not a local reference, it is a leeching reference and an error image is returned to the client. { Context. response. expires = 0; // set the file expiration time in the client buffer to 0, that is, it expires immediately. Context. response. Clear (); // clear the output cache opened by the server for this session Context. response. contenttype = getcontenttype ("error.jpg"); // obtain the file type Context. response. writefile ("error.jpg"); // write the image file with special report errors to the output cache opened by the server for this session Context. response. End (); // transmits the information in the output cache opened by the server for this session to the client. } } |
5. When everything is done, we need to let IIS know and process image files.
We also need to let IIS know about this. why is the aspx file processed? The default httphandler that can process aspx is actually because IIS automatically loadsC:/Windows/Microsoft. NET/framework/v1.1.4322/aspnet_isapi.dllThis file is used to process the ASPX page and is a part of the. NET Framework. We need IIS to load this DLL file for it when it receives a JPG file request.
First, add a processing tool. You can configure the JPG file by referring to the configuration of the aspx file. We also load the aspnet_isapi.dll file for the JPG file, because the processing of JPG files is also based on ASP.. net. Specific configuration, such:
OK. Now you have some knowledge about this method. In fact, there are other custom httphandler applications. We will not go into details here. The following work requires more practices from readers, learn more and gain more experience to better apply the design and development requirements.