【IT168技术文档】
当一个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 e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; response.Write("我来自自定义HttpModule中的BeginRequest<br />"); } private void Application_EndRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; response.Write("我来自自定义HttpModule中的EndRequest<br />"); } #region IHttpModule 成员 public void Dispose() {} public void Init(HttpApplication application) { application.BeginRequest += new EventHandler(Application_BeginRequest); application.EndRequest += new EventHandler(Application_EndRequest); } #endregion } }