By default, IIS directly extracts resources for files such as http request images and sends them to the client. We need to create our own HttpHandler to process image files.
How to implement:
1. Create your own HttpHandler
Create a new class (MyHttpHandler) to inherit the System. Web. IHttpHandler interface and implement the corresponding method
- IsReusable attribute (a returned value indicates whether other http requests can use the instance that currently inherits the System. Web. IHttpHander Interface Class );
- ProcessRequest (System. Web. HttpContext context) method (used to process custom special http requests to be processed ).
Parameters
An instance of the 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
In
Add registration information to Web. config
<HttpHandlers> <br/> <add verb = "*" path = "*. jpg" type = "MyHttpHandler, MyHttpHandler"/> <br/> </pttpHandlers>
3. Now let's lookProcessRequest (System. Web. HttpContext
Context)The method is to process the http request for image files.
Public void ProcessRequest (HttpContext context) <br/>{< br/> // determines whether the request is a local reference. If yes, the system returns the correct image to the client, the judgment here is the reference page information recorded in the preceding http request <br/> if (context. request. urlReferrer. host = "localhost") {<br/> // set the file expiration time in the client buffer to 0, that is, it expires immediately. <Br/> context. response. expires = 0; <br/> // clear the output cache opened by the server for this session <br/> context. response. clear (); <br/> // obtain the file type <br/> context. response. contentType = getContentType (context. request. physicalPath); <br/> // write the request file to the output cache opened by the server for this session <br/> context. response. writeFile (context. request. physicalPath); <br/> // transmits the information in the output cache opened by the server for this session to the client. <br/> context. response. end (); <br/>}< br/> else // if it is not a local reference, it is a leeching reference, error image returned to the client <br/>{< br/> context. response. expires = 0; context. response. clear (); <br/> context. response. contentType = "image/jpg"; <br/> context. response. writeFile (context. request. physicalApplicationPath + "error.jpg"); <br/> context. response. end (); <br/>}< br/>}
5. finally, in the "configuration" of the default website in IIS, add the executable file mapped to the "Application ing" as the Framework installation path \ aspnet_isapi.dll "and the extension is ". jpg.