public class CustomHandler:IHttpHandler{ #region IHttpHandler Members public bool IsReusable { get { throw new NotImplementedException(); } } public void ProcessRequest(HttpContext context) { //获取文件服务器端的物理路径 string FileName = context.Server.MapPath(context.Request.FilePath); // 如果UrlReferrer为空,则显示一张默认的禁止盗链的图片 if (context.Request.UrlReferrer.Host == null){ context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/error.jpg"); }else{ // 如果UrlReferrer中不包含自己站点主机域名,则显示一张默认的禁止盗链的图片 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"); } } #endregion } 接下来在Web Config中注册 该类型 用来解析该类型 解析类型一般通过IIS的aspnet_isapi(服务应用程序编程接口)来识别 <HttpHandlers> verb表示请求的方法 GET or post path为类型 <add verb="*" path="*.jpeg" type='MyNameSpace.MyClass,MyDllName'> </HttpHandlers>