1.创建一个工程,工程下面images文件夹里有jpg图片资源
2.创建第二个工程,要引用上一个工程的jpg图片资源
3.两个工程各自创建一个aspx页面,插入图片资源,另一个插入图片地址
4.创建一个HttpHandler类,编写HttpHandler类
namespace Entity_Framework
{
public class handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//获取上次请求的Url
Uri lastUrl = context.Request.UrlReferrer;
//获取本次请求的Url
Uri currUrl = context.Request.Url;
//判断是否为盗链
if (lastUrl.Host != currUrl.Host || lastUrl.Port != currUrl.Port)
{
//获取警告图片的图片路径
string errorPath = context.Request.PhysicalApplicationPath + "Error/a5.jpg";
//发送到客户端
context.Response.WriteFile(errorPath);
}
else
{
context.Response.WriteFile(context.Request.PhysicalPath);
}
}
public bool IsReusable
{
get { return true; }
}
}
}
5.配置相关配置文件
在Web.config里配置文件请求images文件夹中的jpg图片
<system.webServer>
<handlers>
<add verb="*" path="images/*" type="Entity_Framework.handler" name="plink"/>
</handlers>
</system.webServer>
6.访问网站
注意:设置同时启动两个项目