HttpModule对象
HttpModule的概述和使用
对于HTTP请求而言,HttpModule是HTTP请求的必经之路,能在HTTP请求传递到最终的“请求”做处理之前进行一些额外的工作…
通过一个简单的Web应用来说明HttpModule的用法
首先创建一个网站,然后添加两个ASP.NET页面
1,创建一个HttpModule类
namespace Exampele1
{
public class HttpModule : IHttpModule
{
public void Dispose()
{
throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
//初始化,为HttpApplication绑定两个事件
//HttpHandle处理前的事件
context.BeginRequest += Context_BeginRequest;
//HttpHandle处理后的事件
context.EndRequest += Context_EndRequest;
}
//在处理之前附加信息
private void Context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
application.Response.Write("开始处理请求");
}
//在处理之后附加信息
private void Context_EndRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
application.Response.Write("结束处理请求");
}
}
}
2,配置配置信息,实现了HttpModule类,还要在web.config中配置
<system.webServer>
<modules>
<add name="test" type="Example1.TestHttpMoudle"/>
</modules>
</system.webServer>
3、在创建的两个asp.net页面的源文件中,分别在各自的标签下添加
第一个:欢迎来到页面1
第二个:欢迎来到页面2
最后运行一下