·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> ASP.NET-自定义HttpModule与HttpHandler
在之前的ASP.NET是如何在IIS下工作的这篇文章中介绍了ASP.NET与IIS配合工作的机制,在http请求经过一系列处理后,最后到达ASP.NET管道中,这时,就是Http Modules和HttpHandler出场的时候了。
再来摆出管道工作时序图来一看:
HttpModule
HttpModule是类似于过滤器的作用,可以没有,也可以有任意个,每一个都可以订阅管道事件中的任意个事件,在每个订阅的事件中可自定义功能实现。
HttpModule是实现IHttpModule接口的类。接口如下:
public interface IHttpModule { // 摘要: // 处置由实现 System.Web.IHttpModule 的模块使用的资源(内存除外)。 void Dispose(); // // 摘要: // 初始化模块,并使其为处理请求做好准备。 // // 参数: // context: // 一个 System.Web.Httpapplication,它提供对 ASP.NET 应用程序内所有应用程序对象的公用的方法、属性和事件的访问 void Init(HttpApplication context); }
下面实现一个HttpModule,并订阅管道中的一系列事件,订阅事件就是在Init方法中绑定EventHandler的过程:
代码有点长,因为我把每一个事件都订阅了,这样一来可以清楚的看出哪些事件执行了,这些事件执行的先后顺序是什么。代码如下:
public class MyModule : IHttpModule { #region IHttpModule Members public void Dispose() { //此处放置清除代码。 } public void Init(HttpApplication context) { // 下面是如何处理 LogRequest 事件并为其 // 提供自定义日志记录实现的示例 context.LogRequest += new EventHandler(OnLogRequest); context.BeginRequest += new EventHandler(context_BeginRequest); context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest); context.AcquireRequestState += new EventHandler(context_AcquireRequestState); context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest); context.Disposed += new EventHandler(context_Disposed); context.Error += new EventHandler(context_Error); context.EndRequest += new EventHandler(context_EndRequest); context.MaPRequestHandler += new EventHandler(context_MapRequestHandler); context.PostAcquireRequestState += new EventHandler(context_PostAcquireRequestState); context.PostAuthenticateRequest += new EventHandler(context_PostAuthenticateRequest); context.PostAuthorizeRequest += new EventHandler(context_PostAuthorizeRequest); context.PostLogRequest += new EventHandler(context_PostLogRequest); context.PostReleaseRequestState += new EventHandler(context_PostReleaseRequestState); context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute); context.PostResolveRequestCache += new EventHandler(context_PostResolveRequestCache); context.PostUpdateRequestCache += new EventHandler(context_PostUpdateRequestCache); context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState); context.RequestCompleted += new EventHandler(context_RequestCompleted); context.ResolveRequestCache += new EventHandler(context_ResolveRequestCache); context.UpdateRequestCache += new EventHandler(context_UpdateRequestCache); context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute); context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent); context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders); context.PostMapRequestHandler += new EventHandler(context_PostMapRequestHandler); } void context_Error(object sender, EventArgs e) { WriteLog("Error"); //HttpContext.Current.Response.Write("Error<br />"); } void context_UpdateRequestCache(object sender, EventArgs e) { WriteLog("UpdateRequestCache"); //HttpContext.Current.Response.Write("UpdateRequestCache<br />"); } void context_ResolveRequestCache(object sender, EventArgs e) { WriteLog("ResolveRequestCache"); // HttpContext.Current.Response.Write("ResolveRequestCache<br />"); } void context_RequestCompleted(object sender, EventArgs e) { WriteLog("RequestCompleted"); // HttpContext.Current.Response.Write("RequestCompleted<br />"); } void context_ReleaseRequestState(object sender, EventArgs e) { WriteLog("ReleaseRequestState"); //HttpContext.Current.Response.Write("ReleaseRequestState<br />"); } void context_PostUpdateRequestCache(object sender, EventArgs e) { WriteLog("PostUpdateRequestCache"); //HttpContext.Current.Response.Write("PostUpdateRequestCache<br />"); } void context_PostResolveRequestCache(object sender, EventArgs e) { WriteLog("PostResolveRequestCache"); //HttpContext.Current.Response.Write("PostResolveRequestCache<br />"); } void context_PostRequestHandlerExecute(object sender, EventArgs e) { WriteLog("PostRequestHandlerExecute"); //HttpContext.Current.Response.Write("PostRequestHandlerExecute<br />"); } void context_PostReleaseRequestState(object sender, EventArgs e) { WriteLog("PostReleaseRequestState"); //HttpContext.Current.Response.Write("PostReleaseRequestState<br />"); } void context_PostLogRequest(object sender, EventArgs e) { WriteLog("PostLogRequest"); //HttpContext.Current.Response.Write("PostLogRequest<br />"); } void context_PostAuthorizeRequest(object sender, EventArgs e) { WriteLog("PostAuthorizeRequest"); //HttpContext.Current.Response.Write("PostAuthorizeRequest<br />"); } void context_PostAuthenticateRequest(object sender, EventArgs e) { WriteLog("PostAuthenticateRequest"); //HttpContext.Current.Response.Write("PostAuthenticateRequest<br />"); } void context_PostAcquireRequestState(object sender, EventArgs e) { WriteLog("PostAcquireRequestState"); //HttpContext.Current.Response.Write("PostAcquireRequestState<br />"); } void context_MapRequestHandler(object sender, EventArgs e) { WriteLog("MapRequestHandler"); //HttpContext.Current.Response.Write("MapRequestHandler<br />"); } void context_Disposed(object sender, EventArgs e) { WriteLog("Disposed"); //HttpContext.Current.Response.Write("Disposed<br />"); } void context_AuthorizeRequest(object sender, EventArgs e) { WriteLog("AuthorizeRequest"); //HttpContext.Current.Response.Write("AuthorizeRequest<br />"); } void context_AcquireRequestState(object sender, EventArgs e) { WriteLog("AcquireRequestState"); //HttpContext.Current.Response.Write("AcquireRequestState<br />"); } void context_PreSendRequestHeaders(object sender, EventArgs e) { WriteLog("PreSendRequestHeaders"); //HttpContext.Current.Response.Write("PreSendRequestHeaders<br />"); } void context_PreSendRequestContent(object sender, EventArgs e) { WriteLog("PreSendRequestContent"); //HttpContext.Current.Response.Write("PreSendRequestContent<br />"); } void context_PreRequestHandlerExecute(object sender, EventArgs e) { WriteLog("PreRequestHandlerExecute"); //HttpContext.Current.Response.Write("PreRequestHandlerExecute<br />"); } void context_EndRequest(object sender, EventArgs e) { WriteLog("EndRequest"); //HttpContext.Current.Response.Write("EndRequest<br />"); } void context_BeginRequest(object sender, EventArgs e) { Wr