ASP.NET应用程序与页面生命周期

ASP.NET应用程序与页面生命周期

转载自:http://www.cnblogs.com/suizhouqiwei/archive/2012/08/15/2637775.html

在本文中,我们将了解不同的事件,ASP.NET 应用程序的生命周期以浏览器向 Web 服务器(对于 ASP.NET 应用程序,通常为 IIS)发送请求为起点,直至将请求结果返回至浏览器结束。在这个过程中,首先我们需要了解ASP.NET请求的2个大致的步骤。其次我们将详细了解 'httphandler ',' httpmodule和 asp.net 页面对象(Page)中不同的事件的执行顺序,逻辑。

二个步骤的过程:

asp.net请求处理,2步的过程如下所示,用户发送一个请求到IIS 服务器:

1、asp.net创建一个运行时,可以处理请求。换句话说,它创建应用程序对象,请求,响应和上下文对象处理请求。

2、运行时一旦被创建,请求处理,通过一系列的事件处理模块,Handler处理和页面对象。简称MHPM (Module, handler, page and Module event)。

ASP.NET应用程序与页面生命周期

ASP.NET 应用程序生命周期的各个阶段:

步骤1:ASP.NET 处理已映射到其上的文件扩展名,如 .aspx、.ascx、.ashx 和 .asmx。

步骤2:在应用程序域中,将为名为 HostingEnvironment 的类创建一个实例,该实例提供对有关应用程序的信息(如存储该应用程序的文件夹的名称)的访问。

ASP.NET应用程序与页面生命周期

步骤3:HttpResponse 对象包含发送到客户端的响应,包括所有呈现的输出和 Cookie。

 步骤4:

 不过,为了尽可能提高性能,可对多个请求重复使用 HttpApplication 实例。

ASP.NET应用程序与页面生命周期

 
步骤5:

ASP.NET应用程序与页面生命周期

ASP.NET应用程序与页面生命周期

使用MHPM (Module, handler, page and Module event) 事件处理请求  一旦 HttpApplication对象创建,在处理该请求时将由 HttpApplication 类执行。我们来了解下HttpModule HttpHandlers。

1、如果你想通过*.aspx,*.html文件来处理程序逻辑,那么你需要使用HttpHandler,换句话说,httphandler是一个扩展的处理器。

ASP.NET应用程序与页面生命周期

2、如果你想使用ASP.NET管道来处理程序逻辑,你需要使用HttpModule 换句话说,httpmodule是一个事件处理器。

(处理HttpApplication的事件
       HttpApplication提供了基于事件的扩展机制,运行程序员借助于处理管道中的事件进行处理过程扩展。由于HttpApplication对象是由ASP.NET基础架构来创建和维护的,那么,如何才能获取这个对象引用,以便于注册HttpApplication对象的事件处理,就是程序员首先要解决的问题。在ASP.NET中,提高了两种方式来解决这个问题:IHttpModule方式和 global.asax方式。这两种方式的核心都是IHttpModule接口。

通过IHttpModule创建HttpApplication的事件处理程序
       在ASP.NET中,定义在System.Web命名空间下的IHttpModule接口专门用来定义HttpApplication对象的事件处理。
      实现IHttpModule接口的类成为HttpModule。IHtpModule接口的定义如下,其中仅仅包含两个成员: 

          public interface IHttpModule

         {

void Dispose()
vodi Init(HttpApplication context)

}

创建和注册自定义 HTTP 模块 MSDN:http://msdn.microsoft.com/zh-cn/library/ms227673.aspx      
其中,Dispose方法用于回收Module所使用的非托管资源,如果没有的话,直接返回即可。
最重要的是Init方法,可以看到,这个方法接受一个HttpApplication类型的参数,在ASP.NET中,每当创建一个HttpApplication对象实例,
将遍历注册的HttpModule类型,通过反射,依次创建每个注册HttpModule类型的实例对象,并将这个HttpApplication实例通过Init方法传递给各个HttpModule,
这样HttpModule就可以在第一时间完成针对HttpApplication对象的事件注册了。
例如,希望写一个处理PostAuthentiacteRequest事件的HttpModule,那么,可以如下完成事件的注册:
01.public void Init(HttpApplication context) 
02.       { 
03.           context.PostAuthorizeRequest += new EventHandler(context_PostAuthorizeRequest); 
04.       } 

详见:http://blog.csdn.net/sky1069/article/details/6659667

   )

如何处理请求。mhpm有4个重要的步骤:

步骤1(M: HttpModule):客户端开始请求处理。在asp.net引擎和httpmodule中的事件可以用来处理用户自己定义的逻辑。有6个重要的事件,你可以在你的页面对象被创建之前调用 Begin Requestauthenticaterequestauthorizerequestresolverequestcacheacquirerequeststateprerequesthandlerexecute

步骤2(H: ‘HttpHandler’):一旦上述6个事件被执行完,asp.net引擎调用processrequest事件,如果已实现httphandler在您的项目。

步骤3(P: ASP.NET page):一旦httphandler执行,asp.net页面对象被创建。而asp.net页面对象被创建,Page对象的事件将被调用,这可以帮助我们页面中定制自定义逻辑的处理。有6个重要的事件,Init, Load, validate, event, render ,unload 简称 SILVER   S – Start (does not signify anything as such just forms the word) , I – (Init) , L (Load) , V (Validate), E (Event) and R (Render)。
步骤4(M: HttpModule):一旦页面对象是执行和从内存中卸载,httpmodule提供网页执行事件可以用来注入自定义处理逻辑。有4个重要的后处理事件postrequesthandlerexecutereleaserequeststateupdaterequestcacheendrequest

ASP.NET应用程序与页面生命周期

 

 事件详细介绍:

 
部分 Event事件 Description描述
HttpModule BeginRequest 此事件始终被引发,并且始终是请求处理期间发生的第一个事件。
HttpModule AuthenticateRequest AuthenticateRequest 事件发出信号表示配置的身份验证机制已对当前请求进行了身份验证。
HttpModule AuthorizeRequest 安全模块已验证用户授权时发生。AuthorizeRequest 事件发出信号表示 ASP.NET 已对当前请求进行了授权。
HttpModule ResolveRequestCache 在 ASP.NET 完成授权事件以使缓存模块从缓存中为请求提供服务后发生,从而绕过事件处理程序(例如某个页或 XML Web services)的执行。
HttpModule AcquireRequestState AcquireRequestState 事件在创建了事件处理程序之后引发。
HttpModule PreRequestHandlerExecute 恰好在 ASP.NET 开始执行事件处理程序(例如,某页或某个 XML Web services)前发生。
HttpHandler ProcessRequest Httphandler逻辑被执行。 在本节中,我们会写,需要将每页扩展执行的逻辑。
Page Init

OnInit 方法执行创建 Page 实例所需的初始化和设置步骤。不一定会创建其他服务器控件,也不一定能够访问它们

Page Load ASP.NET控件完成加载,你写的UI操作逻辑或任何其他逻辑在这里。
Page Validate 如果你有你的页面上有验证逻辑,这里检查是否符合验证。
  Render 将页面最终的输出发送到浏览器,如果你想做出一些最终的HTML的变化,这是输出到浏览器,你可以在这里输入你的HTML的逻辑。
Page Unload 页面对象从内存中卸载。
HttpModule PostRequestHandlerExecute 在 ASP.NET 事件处理程序(例如,某页或某个 XML Web service)执行完毕时发生。
HttpModule ReleaserequestState ReleaseRequestState 事件以后,应用程序以该请求结束,并发出 ASP.NET 信号以存储该请求状态。
HttpModule UpdateRequestCache Before you end, if you want to update your cache.结束之前,如果您想更新您的高速缓存。
HttpModule EndRequest 当 ASP.NET 执行完事件处理程序以使缓存模块存储将用于从缓存为后续请求提供服务的响应时发生。

演示代码:

  在这段代码中,我们创建HttpModule 和Httphandler  添加所有请求响应的事件, 下面用 HttpModule Httphandler  跟踪所有活动,并把它添加到一个全局性的集合变量中。

ASP.NET应用程序与页面生命周期
 1  public class clsHttpModule : IHttpModule
 2     {
 3         private HttpApplication httpApp;
 4         public static ArrayList objArrayList = new ArrayList();
 5         public clsHttpModule()
 6         {
 7             
 8         }
 9 
10 
11         public void Dispose()
12         {
13             
14         }
15 
16         public void Init(HttpApplication context)
17         {
18             this.httpApp = context;
19             httpApp.Context.Response.Clear();
20             objArrayList.Clear();
21             objArrayList.Add("httpModule:Init");
22             httpApp.AuthenticateRequest += new EventHandler(OnAuthentication);
23             httpApp.AuthorizeRequest += new EventHandler(OnAuthorization);
24             httpApp.BeginRequest += new EventHandler(OnBeginrequest);
25             httpApp.EndRequest += new EventHandler(OnEndRequest);
26             httpApp.ResolveRequestCache += new EventHandler(OnResolveRequestCache);
27             httpApp.AcquireRequestState += new EventHandler(OnAcquireRequestState);
28             httpApp.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
29             httpApp.PostRequestHandlerExecute += new EventHandler(OnPostRequestHandlerExecute);
30             httpApp.ReleaseRequestState += new EventHandler(OnReleaseRequestState);
31             httpApp.UpdateRequestCache += new EventHandler(OnUpdateRequestCache);
32         }
33         void OnUpdateRequestCache(object sender, EventArgs a)
34         {
35             objArrayList.Add("httpModule:OnUpdateRequestCache");
36         }
37         void OnReleaseRequestState(object sender, EventArgs a)
38         {
39             objArrayList.Add("httpModule:OnReleaseRequestState");
40         }
41         void OnPostRequestHandlerExecute(object sender, EventArgs a)
42         {
43             objArrayList.Add("httpModule:OnPostRequestHandlerExecute");
44         }
45         void OnPreRequestHandlerExecute(object sender, EventArgs a)
46         {
47             objArrayList.Add("httpModule:OnPreRequestHandlerExecute");
48         }
49         void OnAcquireRequestState(object sender, EventArgs a)
50         {
51             objArrayList.Add("httpModule:OnAcquireRequestState");
52         }
53         void OnResolveRequestCache(object sender, EventArgs a)
54         {
55             objArrayList.Add("httpModule:OnResolveRequestCache");
56         }
57         void OnAuthorization(object sender, EventArgs a)
58         {
59             objArrayList.Add("httpModule:OnAuthorization");
60         }
61         void OnAuthentication(object sender, EventArgs a)
62         {
63 
64             objArrayList.Add("httpModule:AuthenticateRequest");
65         }
66         void OnBeginrequest(object sender, EventArgs a)
67         {
68 
69             objArrayList.Add("httpModule:BeginRequest");
70         }
71         void OnEndRequest(object sender, EventArgs a)
72         {
73             objArrayList.Add("httpModule:EndRequest");
74             objArrayList.Add("<hr>");
75             foreach (string str in objArrayList)
76             {
77                 httpApp.Context.Response.Write(str + "<br>");
78             }
79 
80         }
81 
82     }
ASP.NET应用程序与页面生命周期

以下代码片段是跟踪 HttpHandlerProcessRequest '事件' :

ASP.NET应用程序与页面生命周期
 1 public class clsHttpHandler : IHttpHandler
 2     {
 3 
 4         public bool IsReusable
 5         {
 6             get { return true; }
 7         }
 8 
 9         public void ProcessRequest(HttpContext context)
10         {
11             clsHttpModule.objArrayList.Add("HttpHandler:ProcessRequest");
12             context.Response.Redirect("~/Default.aspx");
13         }
14     }
ASP.NET应用程序与页面生命周期

页面Page事件

ASP.NET应用程序与页面生命周期
 1  public partial class _Default : System.Web.UI.Page
 2     {
 3         protected void Page_init(object sender, EventArgs e)
 4         {
 5             clsHttpModule.objArrayList.Add("Page:Init");
 6         }
 7         protected void Page_Load(object sender, EventArgs e)
 8         {
 9             clsHttpModule.objArrayList.Add("Page:Load");
10         }
11         public override void Validate()
12         {
13             clsHttpModule.objArrayList.Add("Page:Validate");
14         }
15         protected void Button1_Click(object sender, EventArgs e)
16         {
17             clsHttpModule.objArrayList.Add("Page:Event");
18         }
19         protected override void Render(HtmlTextWriter output)
20         {
21             clsHttpModule.objArrayList.Add("Page:Render");
22             base.Render(output);
23         }
24         protected void Page_Unload(object sender, EventArgs e)
25         {
26             clsHttpModule.objArrayList.Add("Page:UnLoad");
27         }
28     }
ASP.NET应用程序与页面生命周期

 webconfig中配置:

HttpModules配置
1  <httpModules>
2       <add name="clsHttpModule" type="WebDemo1.Common.clsHttpModule"/>
3     </httpModules>

页面第一次加载效果:

ASP.NET应用程序与页面生命周期

点击button按钮后:

ASP.NET应用程序与页面生命周期

HttpModule

HttpModule通过在某些事件中注册,把自己插入ASP.NET请求处理管道。当这些事件发生的时候,ASP.NET调用对相应的HTTP模块,这样该模块就能处理请求了。

常用操作:

1、向每个页面动态添加一些备注或说明性的文字: 2、判断用户登录

如果定义了多个HttpModule,在web.config文件中引入自定义HttpModule的顺序就决定了多个自定义HttpModule在处理一个HTTP请求的接管顺序。

HttpHandler HttpHandler是HTTP请求的处理中心,真正地对客户端请求的服务器页面做出编译和执行,并将处理过后的信息附加在HTTP请求信息流中再次返回到HttpModule中。 HttpHandler与HttpModule不同,一旦定义了自己的HttpHandler类,那么它对系统的HttpHandler的关系将是“覆盖”关系。

为了验证请求是否进入HttpHandler ,我们可以在WebConfig中 加入:

<add verb="*" path="*.aspx" type="WebDemo1.Common.clsHttpHandler,WebDemo1"/>

调试程序看看。

综上我们可以总计出一张图:

ASP.NET应用程序与页面生命周期

ASP.NET页面事件

     在上面的部分,我们已经看到了一个ASP.NET页面完整的请求事件。 最重要的部分之一,是ASP.NET页面,我们没有详细讨论相同。我们详细讨论下ASP.NET的页面事件。任何ASP.NET页面中有2个部分 如图:

ASP.NET应用程序与页面生命周期

注意 :大部分的开发者直接使用page_load方法的一切,这不是一个好的做法。例如 填充控件,设置视图状态,应用主题等,这些都是发生在页面上的负荷。因此,我们可以在适当的事件,添加适当的逻辑,这将真正使你的代码整洁 符合逻辑。

 
序号 事件  控件初始化 状态 是否 可用 表单数据是否 可用 什么逻辑可以写在这里?
1 Init No No No

不一定会创建其他服务器控件,也不一定能够访问它们。

Load view state Not guaranteed Yes Not guaranteed 您可以访问视图状态和任何同步逻辑
3 PostBackdata Not guaranteed Yes Yes  你可以访问表单数据。
Load Yes Yes Yes 绑定控件 等等。。。
Validate Yes Yes Yes 如果你的页面有验证,或者你想为您的网页执行验证。
6 Event Yes Yes Yes 如果这是通过点击一个按钮或下拉的变化,那么相对应的事件将被执行。
Pre-render Yes Yes Yes 如果你想在这些控件视图状态最后保存之前修改UI对象的结构或属性值。
8 Save view state Yes Yes Yes 一旦所有服务器控件的变化完成,这一事件控制数据保存在视图状态。
Render Yes Yes Yes 如果你想添加一些自定义的HTML输出。
10  Unload Yes Yes Yes

你可以在这里做任何清理。

ASP.NET应用程序与页面生命周期