1、asp.net的HTTP请求处理过程
说明: (1)、客户端浏览器向服务器发出一个http请求,此请求会被inetinfo.exe进程截获,然后转交给aspnet_isapi.dll进程,接着它又通过Http Pipeline的管道,传送给aspnet_wp.exe这个进程,接下来就到了.net framework的HttpRunTime处理中心,处理完毕后就发送给用户浏览器。 (2)、当一个http请求被送入到HttpRuntime之后,这个Http请求会继续被送入到一个被称之为HttpApplication Factory的一个容器当中,而这个容器会给出一个HttpApplication实例来处理传递进来的http请求,而后这个Http请求会依次进入到如下几个容器中:HttpModule --> HttpHandler Factory --> HttpHandler。当系统内部的HttpHandler的ProcessRequest方法处理完毕之后,整个Http Request就被处理完成了,客户端也就得到相应的东东了。 (3)完整的http请求在asp.net framework中的处理流程: HttpRequest-->inetinfo.exe->ASPNET_ISAPI.DLL-->Http Pipeline-->ASPNET_WP.EXE-->HttpRuntime-->HttpApplication Factory-->HttpApplication-->HttpModule-->HttpHandler Factory-->HttpHandler-->HttpHandler.ProcessRequest() ps:红色的HttpApplication实例在HttpModule的Init方法中会用到。 (4)如果想在中途截获一个httpRequest并做些自己的处理,就应该在HttpRuntime运行时内部来做到这一点,确切的说是在HttpModule这个容器中来实现。 2、HttpModule工作原理 负责监听HttpRequest,同时对HttpRequest增添或者过滤掉一部分内容。也就是说,当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于HTTP请求来讲,HttpModule是一个HTTP请求的“必经之路”,所以可以在这个HTTP请求传递到真正的请求处理中心(HttpHandler)之前附加一些需要的信息在这个HTTP请求信息之上,或者针对截获的这个HTTP请求信息作一些额外的工作,或者在某些情况下干脆终止满足一些条件的HTTP请求,从而可以起到一个Filter过滤器的作用。 HttpModule实现了接口IHttpModule,我们可以自定义实现该接口的类,从而取代HttpModule。 asp.net默认的HttpModule如下:
System.Web.SessionState.SessionStateModule;
System.Web.Security.WindowsAuthenticationModule;
System.Web.Security.FormsAuthenticationModule;
System.Web.Security.PassportAuthenticationModule;
System.Web.Security.UrlAuthorizationModule;
System.Web.Security.FileAuthorizationModule;
3、编写自己的HttpModule 要实现HttpModule,必须实现接口IHttpModule。下面是IHttpModule接口分析:
using System;
namespace System.Web
{
// Summary:
// Provides module initialization and disposal events to the implementing class.
public interface IHttpModule
{
// Summary:
// Disposes of the resources (other than memory) used by the module that implements
// System.Web.IHttpModule.
// 销毁不再被HttpModule使用的资源
void Dispose();
//
// Summary:
// Initializes a module and prepares it to handle requests.
//
// Parameters:
// context:
// An System.Web.HttpApplication that provides access to the methods, properties,
// and events common to all application objects within an ASP.NET application
// 初始化一个Module,为捕获HttpRequest做准备
void Init(HttpApplication context);
}
}
下面是自己的HttpModule:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web; //要在自己的类库中添加System.Web引用
namespace MyHttpModule
{
class MySelfHttpModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(Application_BeginRequest);
application.EndRequest += new EventHandler(Application_EndRequest);
}
// 自己要针对一些事情进行处理的两个方法
private void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
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 = sender as HttpApplication;
HttpContext context = application.Context;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
response.Write("我来自自定义HttpModule中的EndRequest<br />");
}
}
}
最后在web项目中使用自己的HttpModule: (1)添加引用
a、不需要在整个web项目添加对类库的引用,只是复制一份到bin目录下即可。 b、default.aspx的cs文件:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<br/>来自Default.aspx页面<br/>");
}
}
(2)web.config的节点配置
IIS 7.0 经典模式以及较早的 IIS 版本
在Web.config的system.web标签中添加:
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
<httpModules>
<add name="MyHttpModuleTest" type="MyHttpModule.MyHttpModule,MyHttpModule" />
</httpModules>
</system.web>
IIS 7.0 集成模式以上。
在Web.config的system.webServer标签中添加:
<system.webServer>
<modules>
<add name="MyHttpModuleTest" type="MyHttpModule.MyHttpModule,MyHttpModule" />
</modules>
</system.webServer>
a、name可以随意指定,没有影响。 b、type有两个参数,第一个表示具体哪个类,第二个表示是哪个dll。 最后运行web项目,你会看到每个页面都会Response.Write两句话出来。 4、HttpModule内部事件机制和生命周期 HttpModule对HttpApplication实例进行处理,而HttpApplication有很多事件(对应不同的生命期),这样就衍生出HttpModule内部事件机制和生命周期。 (1)HttpModule的事件
说明: a、BenginRequest和EndRequest分别是HttpModule容器最开始的和最后的事件; b、EndRequest之后还会触发PreSendRequestHeaders事件和PreSendRequestContent事件,这不是在HttpModule外的两个事件,表示HttpModule结束,即将开始向Client发送数据。 (2)、验证HttpModule生命周期 与HttpHandler的交互:
说明: a、HttpModule容器会将HttpRequest传递到HttpHandler容器,这个时间点是ResolveRequestCache事件 b、HttpModule容器会建立HttpHandler实例作为入口——Session从此生效 c、触发AcquireRequestState事件以及PreRequestHandlerExecute事件 d、HttpModule容器便将对HttpRequest的控制权转让给HttpHandler容器 e、HttpHandler容器处理HttpRequest——使用自身的ProcessRequest方法,将对其控制权又还给HttpModule容器——之后Session失效。 验证生命周期代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHttpModule
{
public class ValidaterHttpModuleEvents : IHttpModule
{
public void Dispose()
{ }
/// <summary>
/// 验证HttpModule事件机制
/// </summary>
/// <param name="application"></param>
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
application.EndRequest += new EventHandler(application_EndRequest);
application.AcquireRequestState += new EventHandler(application_AcquireRequestState);
application.AuthenticateRequest += new EventHandler(application_AuthenticateRequest);
application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);
application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
application.PostRequestHandlerExecute += new EventHandler(application_PostRequestHandlerExecute);
application.ReleaseRequestState += new EventHandler(application_ReleaseRequestState);
application.ResolveRequestCache += new EventHandler(application_ResolveRequestCache);
application.PreSendRequestHeaders += new EventHandler(application_PreSendRequestHeaders);
application.PreSendRequestContent += new EventHandler(application_PreSendRequestContent);
}
private void application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_BeginRequest<br/>");
}
private void application_EndRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_EndRequest<br/>");
}
private void application_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PreRequestHandlerExecute<br/>");
}
private void application_PostRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PostRequestHandlerExecute<br/>");
}
private void application_ReleaseRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_ReleaseRequestState<br/>");
}
private void application_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_AcquireRequestState<br/>");
}
private void application_PreSendRequestContent(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PreSendRequestContent<br/>");
}
private void application_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PreSendRequestHeaders<br/>");
}
private void application_ResolveRequestCache(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_ResolveRequestCache<br/>");
}
private void application_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_AuthorizeRequest<br/>");
}
private void application_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_AuthenticateRequest<br/>");
}
}
}
注:修改配置文件中的httpModules节点中的type对应的类名即可. 5、其他 (1)、加载两个或多个自定义的HttpModule 这里以两个自定义HttpModule举例. 修改配置文件中的httpModules节点: <add name="HttpModule1" type="MyHttpModule.HttpModule1,MyHttpModule"/> <add name="HttpModule2" type="MyHttpModule.HttpModule2,MyHttpModule"/> HttpModule1和HttpModule2模仿ValidaterHttpModuleEvents编写(除了类名改变外,事件和方法不变),不贴代码了。运行结果如下:
分析: a、从运行结果可以看到,在web.config文件中引入自定义HttpModule的顺序就决定了多个自定义HttpModule在处理一个HTTP请求的接管顺序; b、系统默认那几个HttpModule是最先被ASP.NET Framework所加载上去的,对外部是透明的。 (2)、利用HttpModule实现终止此次HttpRequest请求 在BeginRequest事件中,使用HttpApplication.CompleteRequest()方法可以实现当满足一定条件时终止此次HttpRequest请求。看代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHttpModule
{
public class CompleteRequestHttpModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(Application_BeginRequest);
}
private void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("请求<br/>");
application.CompleteRequest(); //在BeginRequest事件中,使用HttpApplication.CompleteRequest()方法实现
application.Context.Response.Write("请求被终止");
}
}
}
说明: a、对于一个HttpModule,在BeginRquest中终止,但是仍然会调用EndRequest事件,以及PreSendRequestHeaders事件和PreSendRequestContent事件。也可以说是直接跳转到EndRequest事件,而不会调用这期间的事件 b、如果有两个HttpModule,在第一个HttpModule的BeginRequest中终止,仅仅不会调用第二个HttpModule的BeginRequest,但仍然会调用两个EndRequest事件,以及PreSendRequestHeaders事件和PreSendRequestContent事件。看下面的图示: