ASP.Net下的HttpModule是基于事件的处理模型,这使得我们在选择事件监听和处理的时候有更多选择。下面是对HttpModule有关事件被触发的监测:
有关代码如下
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Web.SessionState;using System.Threading;/// ///EventTest 的摘要说明/// public class EventTest : IHttpModule, IRequiresSessionState{ public EventTest() { // //TODO: 在此处添加构造函数逻辑 // } void IHttpModule.Dispose() { return; } void IHttpModule.Init(HttpApplication App) { App.AcquireRequestState += new EventHandler(AcquireRequestState); App.BeginRequest += new EventHandler(BeginRequest); App.AuthenticateRequest += new EventHandler(AuthenticateRequest); App.AuthorizeRequest += new EventHandler(AuthorizeRequest); App.Disposed += new EventHandler(Disposed); App.EndRequest += new EventHandler(EndRequest); App.Error += new EventHandler(Error); //App.MapRequestHandler += new EventHandler(MapRequestHandler); App.PostAcquireRequestState += new EventHandler(PostAcquireRequestState); App.PostAuthenticateRequest += new EventHandler(PostAuthenticateRequest); App.PostAuthorizeRequest += new EventHandler(PostAuthorizeRequest); //App.PostLogRequest += new EventHandler(PostLogRequest); App.PostMapRequestHandler += new EventHandler(PostMapRequestHandler); App.PostReleaseRequestState += new EventHandler(PostReleaseRequestState); App.PostRequestHandlerExecute += new EventHandler(PostRequestHandlerExecute); App.PostResolveRequestCache += new EventHandler(PostResolveRequestCache); App.PostUpdateRequestCache += new EventHandler(PostUpdateRequestCache); App.PreRequestHandlerExecute += new EventHandler(PreRequestHandlerExecute); App.PreSendRequestHeaders += new EventHandler(PreSendRequestHeaders); App.PreSendRequestContent += new EventHandler(PreSendRequestContent); App.ReleaseRequestState += new EventHandler(ReleaseRequestState); App.ResolveRequestCache += new EventHandler(ResolveRequestCache); App.UpdateRequestCache += new EventHandler(UpdateRequestCache); } private void BeginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:BeginRequest(执行Http请求管线链中第一个事件时发生)"); } private void AcquireRequestState(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:AcquireRequestState(与当前建立会话时发生)"); if (context.Session["T"] != null) { response.Write(" + " + context.Session["T"].ToString()); } else { response.Write(" Session未收到!"); } } private void AuthenticateRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:AuthenticateRequest(安全模块建立用户标记时发生)"); } private void AuthorizeRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:AuthorizeRequest(安全模块验证用户授权时发生)"); } private void Disposed(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:Disposed(释放应用时发生)"); } private void EndRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:EndRequest(执行Http请求管线链中最后一个事件时发生)"); } private void Error(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + Error(Error事件时发生)"); } private void PostAcquireRequestState(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:PostAcquireRequestState(已经获得当前请求状态时发生)"); if (context.Session["T"] != null) { response.Write(" + " + context.Session["T"].ToString()); } else { response.Write(" Session未收到!"); } } private void PostAuthenticateRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:PostAuthenticateRequest(已建立用户标识时发生)"); } private void PostAuthorizeRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:PostAuthorizeRequest(当前请求的用户已获得授权时发生)"); } private void PostMapRequestHandler(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:PostMapRequestHandler(当前请求事件映射到相应事件后发生)"); } private void PostReleaseRequestState(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + PostReleaseRequestState(完成请求事件并且请求状态已存储时发生)"); } private void PostRequestHandlerExecute(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:PostRequestHandlerExecute(ASP.Net事件执行完毕时发生)"); } private void PostResolveRequestCache(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:PostResolveRequestCache(跳过当前请求并接受来自缓存数据时发生)"); } private void PostUpdateRequestCache(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:PostUpdateRequestCache(事件缓存被更新时发生)"); } private void PreRequestHandlerExecute(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:PreRequestHandlerExecute(页面事件执行前发生)"); if (context.Session["T"] != null) { response.Write(" + " + context.Session["T"].ToString()); } else { response.Write(" Session未收到!"); } } private void PreSendRequestContent(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); //response.Write("" + time + " + IHttpModule:PreSendRequestContent(向客户端发送内容之前发生)"); } private void PreSendRequestHeaders(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:PreSendRequestHeaders(向客户端发送HttP头之前发生)"); } private void ReleaseRequestState(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:ReleaseRequestState(事件执行完成之后状态处理)"); } private void ResolveRequestCache(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:ResolveRequestCache(从缓存中发生数据请求时)"); } private void UpdateRequestCache(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = application.Request; HttpResponse response = application.Response; string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); response.Write("" + time + " + IHttpModule:UpdateRequestCache(时间执行完毕,为缓存新的事件准备)"); }}
需要在web.Config加入
任意Page的代码(aspx)
protected void Page_Load(object sender, EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Response.Write("" + time + " + Page:Page_Load"); } protected void TextBox1_TextChanged(object sender, EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Response.Write("" + time + " + Page:TextBox1_TextChanged"); Label1.Text = TextBox1.Text; } protected void TextBox2_TextChanged(object sender, EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Response.Write("" + time + " + Page:TextBox2_TextChanged"); Label2.Text = TextBox2.Text; } private void Page_LoadComplete(object sender, System.EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Response.Write("" + time + " + Page:Page_LoadComplete"); } private void Page_Unload(object sender, System.EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); //Response.Write("" + time + " + Page_Unload"); } private void Page_PreInit(object sender, System.EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Response.Write("" + time + " + Page:Page_PreInit"); } private void Page_Init(object sender, System.EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Session["T"] = "来自Page级:Page_Init" + time; Response.Write("" + time + " + Page:Page_Init"); } private void Page_InitComplete(object sender, System.EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Response.Write("" + time + " + Page:Page_InitComplete"); } private void Page_PreLoad(object sender, System.EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Response.Write("" + time + " + Page:Page_PreLoad"); } private void Page_PreRender(object sender, System.EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Response.Write("" + time + " + Page:Page_PreRender"); } private void Page_PreRenderComplete(object sender, System.EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Response.Write("" + time + " + Page:Page_PreRenderComplete"); } private void Page_SaveStateComplete(object sender, System.EventArgs e) { string time = DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString("000"); Response.Write("" + time + " + Page:Page_SaveStateComplete"); }
当Page(aspx)页面事件被加载(首次)被加载后的信息
2009-11-22 18:02:35:828 + IHttpModule:BeginRequest(执行Http请求管线链中第一个事件时发生)2009-11-22 18:02:35:828 + IHttpModule:AuthenticateRequest(安全模块建立用户标记时发生)2009-11-22 18:02:35:828 + IHttpModule:PostAuthenticateRequest(已建立用户标识时发生)2009-11-22 18:02:35:828 + IHttpModule:AuthorizeRequest(安全模块验证用户授权时发生)2009-11-22 18:02:35:828 + IHttpModule:PostAuthorizeRequest(当前请求的用户已获得授权时发生)2009-11-22 18:02:35:828 + IHttpModule:ResolveRequestCache(从缓存中发生数据请求时)2009-11-22 18:02:35:828 + IHttpModule:PostResolveRequestCache(跳过当前请求并接受来自缓存数据时发生)2009-11-22 18:02:35:859 + IHttpModule:PostMapRequestHandler(当前请求事件映射到相应事件后发生)2009-11-22 18:02:35:859 + IHttpModule:AcquireRequestState(与当前建立会话时发生) Session未收到!2009-11-22 18:02:35:859 + IHttpModule:PostAcquireRequestState(已经获得当前请求状态时发生) Session未收到!2009-11-22 18:02:35:859 + IHttpModule:PreRequestHandlerExecute(页面事件执行前发生) Session未收到!2009-11-22 18:02:35:875 + Page:Page_PreInit2009-11-22 18:02:35:875 + Page:Page_Init2009-11-22 18:02:35:875 + Page:Page_InitComplete2009-11-22 18:02:35:875 + Page:Page_PreLoad2009-11-22 18:02:35:875 + Page:Page_Load2009-11-22 18:02:35:875 + Page:Page_LoadComplete2009-11-22 18:02:35:875 + Page:Page_PreRender2009-11-22 18:02:35:875 + Page:Page_PreRenderComplete2009-11-22 18:02:35:953 + Page:Page_SaveStateCompleteLabel1(aspx页面的)Label2(aspx页面的)2009-11-22 18:02:35:953 + IHttpModule:PostRequestHandlerExecute(ASP.Net事件执行完毕时发生)2009-11-22 18:02:35:953 + IHttpModule:ReleaseRequestState(事件执行完成之后状态处理)2009-11-22 18:02:35:953 + PostReleaseRequestState(完成请求事件并且请求状态已存储时发生)2009-11-22 18:02:35:953 + IHttpModule:UpdateRequestCache(时间执行完毕,为缓存新的事件准备)2009-11-22 18:02:35:953 + IHttpModule:PostUpdateRequestCache(事件缓存被更新时发生)2009-11-22 18:02:35:953 + IHttpModule:EndRequest(执行Http请求管线链中最后一个事件时发生)2009-11-22 18:02:35:953 + IHttpModule:PreSendRequestHeaders(向客户端发送HttP头之前发生)
注意,想使用Session时,必须在AcquireRequestState,PostAcquireRequestState以及PreRequestHandlerExecute事件中.
页面事件被触发
2009-11-22 18:30:05:843 + IHttpModule:BeginRequest(执行Http请求管线链中第一个事件时发生)2009-11-22 18:30:05:843 + IHttpModule:AuthenticateRequest(安全模块建立用户标记时发生)2009-11-22 18:30:05:843 + IHttpModule:PostAuthenticateRequest(已建立用户标识时发生)2009-11-22 18:30:05:843 + IHttpModule:AuthorizeRequest(安全模块验证用户授权时发生)2009-11-22 18:30:05:843 + IHttpModule:PostAuthorizeRequest(当前请求的用户已获得授权时发生)2009-11-22 18:30:05:843 + IHttpModule:ResolveRequestCache(从缓存中发生数据请求时)2009-11-22 18:30:05:843 + IHttpModule:PostResolveRequestCache(跳过当前请求并接受来自缓存数据时发生)2009-11-22 18:30:05:843 + IHttpModule:PostMapRequestHandler(当前请求事件映射到相应事件后发生)2009-11-22 18:30:05:843 + IHttpModule:AcquireRequestState(与当前建立会话时发生) + 来自Page级:Page_Init2009-11-22 18:30:03:4062009-11-22 18:30:05:843 + IHttpModule:PostAcquireRequestState(已经获得当前请求状态时发生) + 来自Page级:Page_Init2009-11-22 18:30:03:4062009-11-22 18:30:05:843 + IHttpModule:PreRequestHandlerExecute(页面事件执行前发生) + 来自Page级:Page_Init2009-11-22 18:30:03:4062009-11-22 18:30:05:843 + Page:Page_PreInit2009-11-22 18:30:05:843 + Page:Page_Init2009-11-22 18:30:05:843 + Page:Page_InitComplete2009-11-22 18:30:05:843 + Page:Page_PreLoad2009-11-22 18:30:05:843 + Page:Page_Load2009-11-22 18:30:05:843 + Page:TextBox1_TextChanged2009-11-22 18:30:05:843 + Page:Page_LoadComplete2009-11-22 18:30:05:843 + Page:Page_PreRender2009-11-22 18:30:05:843 + Page:Page_PreRenderComplete2009-11-22 18:30:05:843 + Page:Page_SaveStateCompleteaspx 页面信息2009-11-22 18:30:05:843 + IHttpModule:PostRequestHandlerExecute(ASP.Net事件执行完毕时发生)2009-11-22 18:30:05:843 + IHttpModule:ReleaseRequestState(事件执行完成之后状态处理)2009-11-22 18:30:05:843 + PostReleaseRequestState(完成请求事件并且请求状态已存储时发生)2009-11-22 18:30:05:843 + IHttpModule:UpdateRequestCache(时间执行完毕,为缓存新的事件准备)2009-11-22 18:30:05:843 + IHttpModule:PostUpdateRequestCache(事件缓存被更新时发生)2009-11-22 18:30:05:843 + IHttpModule:EndRequest(执行Http请求管线链中最后一个事件时发生)2009-11-22 18:30:05:843 + IHttpModule:PreSendRequestHeaders(向客户端发送HttP头之前发生)
以下为摘录,用于加深事件模型的理解
Page 执行中将按照如下顺序激活事件:Page.PreInitPage.InitPage.InitComplitePage.PreLoadPage.LoadPage.LoadCompletePage.PreRenderPage.PreRenderComplete如果页面从令一个页面继承,如BasePage:System.Web.UI.Page,在BasePage中做了一些扩展,如权限检查,而其他页面从BasePage继承,则BasePage和最终Page的事件激活顺序是:UI.PreInitPage.PreInitUI.InitPage.InitUI.InitComplitePage.InitCompliteUI.PreLoadPage.PreLoadUI.LoadPage.LoadUI.LoadCompletePage.LoadCompleteUI.PreRenderPage.PreRenderUI.PreRenderCompletePage.PreRenderComplete如果使用了MasterPage,则MasterPage中的事件和ContentPage中的事件按照下面顺序激活:ContentPage.PreInitMaster.InitContentPage.InitContentPage.InitCompliteContentPage.PreLoadContentPage.LoadMaster.LoadContentPage.LoadCompleteContentPage.PreRenderMaster.PreRenderContentPage.PreRenderComplete更进一步,如果ContentPage继承BasePage,那么,各事件的执行顺序将变成:UI.PreInitContentPage.PreInitMaster.InitUI.InitContentPage.InitUI.InitCompliteContentPage.InitCompliteUI.PreLoadContentPage.PreLoadUI.LoadContentPage.LoadMaster.LoadUI.LoadCompleteContentPage.LoadCompleteUI.PreRenderContentPage.PreRenderMaster.PreRenderUI.PreRenderCompleteContentPage.PreRenderComplete
MasterPage | UserControlOnTop | Page | UserControlInPage | UserControlOnButtom |
Init | ||||
Init | ||||
Init | ||||
Init | ||||
Init | ||||
Load | ||||
Load | ||||
Load | ||||
Lod | ||||
Load | ||||
ControlEvents | ControlEvents | ControlEvents | ControlEvents | ControlEvents |
PreRender | ||||
PreRender | ||||
PreRender | ||||
PreRender | ||||
PreRender | ||||
UnLoad | ||||
UnLoad | ||||
UnLoad | ||||
UnLoad | ||||
UnLoad |