首先是HttpHandler類的代碼:
using System;using System.Collections.Generic;using System.Web;namespace HttpHandler{ public class JPEGHandler : IHttpHandler { #region IHttpHandler 成員 public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { string fileName = context.Request.FilePath; if (context.Request.UrlReferrer.Host == null) { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/no.jpg"); } else { if (context.Request.UrlReferrer.Host.IndexOf("localhost") >= 0) { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(fileName); } else { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/no.jpg"); } } } #endregion }}
然後是web.config中的代碼:
<configuration><system.web><httpHandlers><add verb="*" path="*.jpg" type="HttpHandler.JPEGHandler"/></httpHandlers></configuration>
最後就是在網站根目錄下放入no.jpg就可以了。