新建一个类 ValidaterHttpModuleEvents继承管道接口 IHttpModule,代码如下
public class ValidaterHttpModuleEvents:IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); context.EndRequest += new EventHandler(context_EndRequest); context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest); context.AcquireRequestState += new EventHandler(context_AcquireRequestState); context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest); context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute); context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute); context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState); context.UpdateRequestCache += new EventHandler(context_UpdateRequestCache); context.ResolveRequestCache += new EventHandler(context_ResolveRequestCache); context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders); context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent); } //1 void context_BeginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("请求处理开始<br>"); } //2 void context_AuthenticateRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("封装请求身份验证<br>"); } //3 void context_AuthorizeRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("封装检查是否能利用以前缓存的输出页面处理请求的过程<br>"); } //4 void context_ResolveRequestCache(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("从缓存中得到数据<br>"); } //5加载初始化Session void context_AcquireRequestState(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("封装坚持是否能利用以前缓存的输出页面处理请求<br>"); } //6 void context_PreRequestHandlerExecute(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("Http请求进入HttpHandler之前触发<br>"); } //7 void context_PostRequestHandlerExecute(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("在Http请求进入HttpHandler之后触发<br>"); } //8 void context_ReleaseRequestState(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("存储Session状态时触发<br>"); } //9 void context_UpdateRequestCache(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("更新缓存信息时触发<br>"); } //10 void context_EndRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("Http请求处理完成<br>"); } //11 void context_PreSendRequestHeaders(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("在向客户端发送Header之前触发<br>"); } //12 void context_PreSendRequestContent(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; application.Context.Response.Write("在向客户端发送内容之前触发<br>"); }
新建一个aspx页面,在Page_Load事件写上
Response.Write("<br/><br/>来自Default.aspx页面<br/>");
最后,在配置文件中加上如下代码
<httpModules>
<add name="MyHttpModule" type="HttpModuleTest.ValidaterHttpModuleEvents,HttpModuleTest"/>
</httpModules>
type中的“HttpModuleTest”表示最基本的命名空间,ValidaterHttpModuleEvents表示自己的HttpModule名称。
运行结果如下图所示