代码
1 public class UrlHandler : IHttpHandler
2 {
3 //指明该HttpHandler的实现类是否需要缓存
4 public bool IsReusable
5 {
6 get { return true; }
7 }
8 public UrlHandler()
9 {
10 }
11 public void ProcessRequest(HttpContext context)
12 {
13 string FileName = context.Server.MapPath(context.Request.FilePath);
14 if (context.Request.UrlReferrer == null)
15 {
16 context.Response.ContentType = "image/JPEG";
17 context.Response.WriteFile("~/no.jpg");//被替换图片
18 }
19 else
20 {
21 if (context.Request.UrlReferrer.Host.IndexOf("localhost") > -1)//这里是你的域名,如www.maticsoft.com
22 {
23 context.Response.ContentType = "image/JPEG";
24 context.Response.WriteFile(FileName);
25 }
26 else
27 {
28 context.Response.ContentType = "image/JPEG";
29 context.Response.WriteFile("~/no.jpg");
30 }
31 }
32 }
33 }
2 {
3 //指明该HttpHandler的实现类是否需要缓存
4 public bool IsReusable
5 {
6 get { return true; }
7 }
8 public UrlHandler()
9 {
10 }
11 public void ProcessRequest(HttpContext context)
12 {
13 string FileName = context.Server.MapPath(context.Request.FilePath);
14 if (context.Request.UrlReferrer == null)
15 {
16 context.Response.ContentType = "image/JPEG";
17 context.Response.WriteFile("~/no.jpg");//被替换图片
18 }
19 else
20 {
21 if (context.Request.UrlReferrer.Host.IndexOf("localhost") > -1)//这里是你的域名,如www.maticsoft.com
22 {
23 context.Response.ContentType = "image/JPEG";
24 context.Response.WriteFile(FileName);
25 }
26 else
27 {
28 context.Response.ContentType = "image/JPEG";
29 context.Response.WriteFile("~/no.jpg");
30 }
31 }
32 }
33 }