- 创建Module类
- 继承IHttpModule(用于Module的作用域使用)
- 编写BeginRequest和EndRequest事件(BeginRequest代表所有事件的开始EndRequest代表着所有事件的结束)
- 在Config配置文件中配置module类
public class Module : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += Begin;
context.EndRequest += End;
}
private void End(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
application.Response.Write("<p>这是开始</p>");
}
private void Begin(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
application.Response.Write("这是结束");
}
}
Request获得用户的请求(接收数据)
response是响应对象的请求(发送数据)
- 在项目中创建一个一般处理类程序Handler.ashx
- 后台代码如下
- lastUrl获取上次的Url currentUrl获取本次请求的url
- 判断Host主机号和Port端口号是否一致
- 在config配置文件中配置Handler1.ashx文件
public void ProcessRequest(HttpContext context)
{
//获取上次请求的Url
Uri lastUrl = context.Request.UrlReferrer;
//获取本次请求的Url
Uri currentUrl = context.Request.Url;
//请求是否为盗链 //Host主机号 Port端口号
if (lastUrl.Host != currentUrl.Host || lastUrl.Port != currentUrl.Port)
{
//获取“请勿盗链”警告提示图片路径
string errorImagePath = context.Request.PhysicalApplicationPath + "images/2.png";
//发送至客户端
context.Response.WriteFile(errorImagePath);
}
else
{
context.Response.WriteFile(context.Request.PhysicalPath);
}
}
public bool IsReusable
{
get
{
return true;
}
}