Use Httphandle to achieve, special processing of requests for picture files
The first step: Create a class, inherit from IHttpHandler, the code is as follows
C # code
usingSystem;usingsystem.web;namespacecustomhandler{ Public classjpghandler:ihttphandler{ Public voidProcessRequest (HttpContext context) {//get the file server-side physical pathstringFileName =context. Server.MapPath (context. Request.filepath);//if Urlreferrer is empty, a picture of the default forbidden hotlinking is displayedif(Context. Request.UrlReferrer.Host = =NULL) {context. Response.ContentType="Image/jpeg"; context. Response.WriteFile ("/error.jpg");}Else{//If the urlreferrer does not contain its own site host domain name, a default forbidden hotlinking picture is displayedif(Context. Request.UrlReferrer.Host.IndexOf ("yourdomain.com") >0) {context. Response.ContentType="Image/jpeg"; context. Response.WriteFile (FileName);}Else{context. Response.ContentType="Image/jpeg"; context. Response.WriteFile ("/error.jpg");}}}
public bool isreusable{
get{return true;}
}
}
}
Step two: Compile into a DLL
Csc/t:library CustomHandler.cs
Step three: Add the compiled DLL reference to the Bin folder of the current site
Fourth Step: Register this handler in Web. config
C # code
< system.web > < httphandlers > < add path = "*.jpg,*.jpeg,*.gif,*.png,*.bmp" verb = "*" type = "Customhandler.jpghandler,customhandler" /> </ httphandlers > </ system.web >
Verb refers to the way the file is requested, which can be a post or get, with * representing all access methods. Customhandler.jpghandler represents the namespace and class name, Customhandler represents the assembly name
Asp. NET implementation picture anti-theft chain (GO)