STEP.1: Create file CustomHandler.cs, code as follows:
Copy Code code as follows:
Using System;
Using System.Web;
Namespace customhandler{
public class jpghandler:ihttphandler{
public void ProcessRequest (HttpContext context) {
To obtain a file server-side physical path
String FileName = context. Server.MapPath (context. Request.filepath);
If Urlreferrer is empty, a picture with a default hotlinking is displayed
if (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 picture with a default ban of hotlinking is displayed
if (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.2 compile this file
Copy Code code as follows:
Csc/t:library/r:system.web.dll CustomHandler.cs
STEP.3 copies the compiled CustomHandler.dll to the Bin directory of the site.
STEP.4 registers this handler in Web.config.
Copy Code code as follows:
<system.web>
<add path= "*.jpg" verb= "*" type= "Customhandler.jpghandler, Customhandler"/>
</system.web>
OK, you can test yourself by the steps.