Step.1: Create the file CustomHandler.cs with the following code:
using System;
using System.Web;
namespace CustomHandler{
public class JpgHandler : IHttpHandler{
public void ProcessRequest(HttpContext context){
// Get the file server side Physical path
string FileName = context.Server.MapPath(context.Request.FilePath);
// If UrlReferrer is empty, a default hotlink-prohibited image will be displayed
if (context.Request .UrlReferrer.Host == null){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}else{
// If the UrlReferrer does not contain the domain name of your own site's host, a default picture that prohibits hotlinking will be 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
csc /t:library /r:System.Web.dll CustomHandler.cs
Step.3 Copy the compiled CustomHandler.dll to the Bin of the site directory.
Step.4 Register this Handler in Web.Config.
OK, you can test it yourself according to the steps, so I won’t go into details here.