HttpModule<?XML:NAMESPACE PREFIX = O />
HttpModule
是如何工作的
当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于HTTP请求来讲,HttpModule是一个HTTP请求的“必经之路”,所以可以在这个HTTP请求传递到真正的请求处理中心(HttpHandler)之前附加一些需要的信息在这个HTTP请求信息之上,或者针对截获的这个HTTP请求信息作一些额外的工作,或者在某些情况下干脆终止满足一些条件的HTTP请求,从而可以起到一个Filter过滤器的作用。
示例1:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHttpModule
{
/// <summary>
/// 说明:用来实现自己的HttpModule类。
/// 作者:文野
/// 联系:stwyhm@cnblogs.com
/// </summary>
public class MyFirstHttpModule : IHttpModule
{
private void Application_BeginRequest(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
HttpContext
HttpRequest
HttpResponse
"我来自自定义HttpModule中的BeginRequest<br />");
}
private void Application_EndRequest(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
HttpContext
HttpRequest
HttpResponse
"我来自自定义HttpModule中的EndRequest<br />");
}
#region IHttpModule 成员
public void
{}
public void Init(HttpApplication
{
new EventHandler(Application_BeginRequest);
new EventHandler(Application_EndRequest);
}
#endregion
}
}
在Web.config进行如下配置
<add name="MyFirstHttpModule" type="MyHttpModule.MyFirstHttpModule,MyHttpModule"/>
<?XML:NAMESPACE PREFIX = V /><shapetype coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></path><lock v:ext="edit" aspectratio="t"></lock></shapetype>
深入了解HttpModule
一个HTTP请求在HttpModule容器的传递过程中,会在某一时刻(ResolveRequestCache事件)将这个HTTP请求传递给HttpHandler容器。在这个事件之后,HttpModule容器会建立一个HttpHandler的入口实例,但是此时并没有将HTTP请求控制权交出,而是继续触发AcquireRequestState事件以及PreRequestHandlerExcute事件。在PreRequestHandlerExcute事件之后,HttpModule窗口就会将控制权暂时交给HttpHandler容器,以便进行真正的HTTP请求处理工作。
而在HttpHandler容器内部会执行ProcessRequest方法来处理HTTP请求。在容器HttpHandler处理完毕整个HTTP请求之后,会将控制权交还给HttpModule,HttpModule则会继续对处理完毕的HTTP请求信息流进行层层的转交动作,直到返回到客户端为止。
图1:HttpModule生命周期示意图
示例2:验证HttpModule生命周期
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHttpModule
{
public class ValidaterHttpModule : IHttpModule
{
#region IHttpModule 成员
public void
{}
public void Init(HttpApplication
{
new EventHandler(application_BeginRequest);
new EventHandler(application_EndRequest);
new EventHandler(application_PreRequestHandlerExecute);
new EventHandler(application_PostRequestHandlerExecute);
new EventHandler(application_ReleaseRequestState);
new EventHandler(application_AcquireRequestState);
new EventHandler(application_AuthenticateRequest);
new EventHandler(application_AuthorizeRequest);
new EventHandler(application_ResolveRequestCache);
new EventHandler(application_PreSendRequestHeaders);
new EventHandler(application_PreSendRequestContent);
}
void application_PreSendRequestContent(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_PreSendRequestContent<br/>");
}
void application_PreSendRequestHeaders(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_PreSendRequestHeaders<br/>");
}
void application_ResolveRequestCache(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_ResolveRequestCache<br/>");
}
void application_AuthorizeRequest(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_AuthorizeRequest<br/>");
}
void application_AuthenticateRequest(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_AuthenticateRequest<br/>");
}
void application_AcquireRequestState(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_AcquireRequestState<br/>");
}
void application_ReleaseRequestState(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_ReleaseRequestState<br/>");
}
void application_PostRequestHandlerExecute(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_PostRequestHandlerExecute<br/>");
}
void application_PreRequestHandlerExecute(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_PreRequestHandlerExecute<br/>");
}
void application_EndRequest(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_EndRequest<br/>");
}
void application_BeginRequest(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
"application_BeginRequest<br/>");
}
#endregion
}
}
多个自定义的Http Module的运作
从运行结果可以看到,在web.config文件中引入自定义HttpModule的顺序就决定了多个自定义HttpModule在处理一个HTTP请求的接管顺序。注:系统默认那几个HttpModule是最先衩ASP.NET Framework所加载上去的。
示例3:(代码类同示例2)
在HttpModule中终止此次的HTTP请求
可以利用HttpModule通过调用HttpApplication.CompleteRequest()方法实现当满足某一个条件时终止此次的HTTP请求。
需要注意的是,即使调用了HttpApplication.CompleteRequest()方法终止了一个HTTP请求,ASP.NET Framework仍然会触发HttpApplication后面的这3个事件:EndRequest事件、PreSendRequestHeaders事件、PreSendRequestContent事件。
如果存在多个自定义的HttpModule的话,当Module1终止了一个HTTP请求,这个HTTP请求将不会再触发Module2中相应的事件了,但Module2的最后三个事件仍会被触发。
示例4:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHttpModule
{
public class CompleteRequestHttpModule : IHttpModule
{
#region IHttpModule 成员
public void
{}
public void Init(HttpApplication
{
new EventHandler(Application_BeginRequest);
}
void Application_BeginRequest(object sender, EventArgs
{
HttpApplication application = (HttpApplication)sender;
application.CompleteRequest();
"请求被终止。");
}
#endregion
}
}
参考资料
《ASP.NET深入解析》
《ASP.NET实用全书》