快捷搜索:

HttpApplication,HttpModule,HttpContext及Asp.Net页生命周期

IIS在接到一个新的http哀求后,终极会调用asp.net_isapi.dll的ISAPI扩展(特指IIS6.0情况,iis7.0的利用法度榜样池默觉得集成要领,相对有所变更),然后通报到httpRuntime Pipe(http运行时管道),Asp.Net这时才开始运行(即HttpRunTime是Asp.Net真正的进口),HttpRunTime会为每个asp.net利用自动创建一个HttpApplication的实例,而该实例中又包孕以下属性:

注1

Application -->相称于传统意义上asp期间的application工具,平日用于定义一个asp.net利用的全局变量

Context -->HttpContext(高低文)类的实例【Asp.Net新增的】

Modules -->影响当前利用法度榜样的HttpModule模块聚拢

Request -->类似于asp中的Request工具,平日用于接管一些特定的值(比如Request.Form或Request.QueryString)

Response -->类似于asp中的Response工具,平日用于向做页面输出指定内容(比如Resonse.Write)

Server -->类似于asp中的Server工具,经由过程它能得到一些办事真个信息(比如Server.MapPath)

Session -->类似于asp中的Session工具

User -->用于获取用户认证相关的安然信息

从上面的属性可以发明:很多其其实asp年代已在应用,只有Context,Modules,User这三个是Asp.Net新增的

HttpApplication类除了具备"注1"的几个属性外,还有自己的措施,这里分外提一下Init措施和Dispose措施,这二个措施均可重载.

它们的调用机会为:

Init措施在Application_Start之后调用,而Dispose在Application_End之前调用,别的Application_Start在全部asp.net利用的生命周期内只引发一次(比如IIS启动或网站启动时),类似的Application_End也只有当asp.net利用法度榜样关闭时被调用(比如IIS竣事或网站竣事时)

除了Application_Start和Application_End措施,HttpApplication还供给了以下事故:

这些事故包括前面提到的可重载的Init及Dispose措施,再加上Session对应的Session_Start与Session_End措施,均可直接在Global.ascx.cs中以Application_XXX的形式应用(由于Global.ascx.cs中定义的类Global本身便是承袭自HttpApplication的)

public class Global : System.Web.HttpApplication

再来看一下相对asp而言,新增的Context,Modules,User这三个属性

Context:

Context即HttpContext类的实例,在险些全部aspx页面生命周期中,Context高低文不停伴跟着各个环节向下通报

以是我们险些可以在web利用中的任何环节,用HttpContext.Current来引用到当前的高低文实例,从HttpContext的定义上,还可以发明Context本身的属性中,又可以获得Application,ApplicationInstance,Profile,Response.Request...等工具的实例引用

回顾一下:

public class Handler1 : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType = "text/plain";

context.Response.Write("Hello World");

}

public bool IsReusable

{

get

{

return false;

}

}

}

我们在应用一个ashx文件时,ProcessRequest措施就是把当前高低文通报进来,进而经由过程context获得Response工具的引用,终极可以向页面输出任何想要的内容.

Modules:

每一个实现了IHttpModule接口的类,就可以被觉得是Http模块组件,可以理解为http哀求拦截器,拦截到http哀求后,它能改动正在被处置惩罚的Context高低文,完事儿之后,再把节制权交还给管道,假如还有其它模块,则依次继承处置惩罚,直到所有Modules聚拢中的HttpModule都“爽”完为止(注:可怜的http哀求就这样给各个httpModule轮X了)

asp.net2.0默认内置了很多HttpModule,从Machine.Config文件中可以发明以下默认的内置模块:

注2

AnonymouseIdentification --为匿名用户分配一个临时身份

FileAuthorization --验证用户是否有哀求文件的Windows NT许可

FormsAuthentication --窗体身份验证模块(假如没有这个模块,asp.net就无法以用户名/密码[即FOrms]要领验证)

OutputCache --输出缓存模块

PassportAuthentication --PassPort验证模块

Profile --用户设置设置设备摆设摆设模块(假如没有它,asp.net中就无法应用Profile)

RoleManager --角色治理

SessionSate --会话状态模块

UrlAuthorization --基于URL的身份验证模块

WindowsAuthentication --Windows和IIS身份验证模块

User:

假如您应用过asp.net2.0内置的Membership/Role机制来进行造访认证,就会对User工具认为很认识,比如:

if (HttpContext.Current.User.Identity.IsAuthenticated)

{

//用户登录过了...

}

我们常用它来判断当前浏览用户的登录状态,关于User类的更具体定义,可拜见MSDN

生命周期:

着末再往返首一下Asp.Net中Page页的生命周期,Page中定义了几个事故:

总体上讲:一个ASPX页面被哀求时,终极的生命周期便是由Page中定义的上述事故(还有一些可重载的回调措施)以及曩昔提到的HttpApplication类中定义的事故(以响应的回调措施)合营触发或调用,终极叠加形成的连续串处置惩罚历程。

假如先不斟酌HttpApplication中的事故处置惩罚措施(即不斟酌我们在Global.ascx.cs中定义的Application_XXX处置惩罚措施),Page中的事故(措施)老例触发(调用)顺序为:

01.Page_PreInit

02.Page_Init

03.Page_InitComplete

04.Page_PreLoad

05.Page_Load

06.Page_LoadComplete

07.Page_PreRender

08.Page_SaveStateComplete

09.Page_Unload

这是在Page页面未回发,且不斟酌页面子控件的条件下正常的顺序,假如加入页面回发(比如在页面中放一个asp:Button,然后在Button的Click回发事故中加入处置惩罚函数)后,顺序轻细有些变更:

01.Page_PreInit

02.Page_Init

03.Page_InitComplete

04.Page_PreLoad

05.Page_Load

06.Button1_Click

07.Page_LoadComplete

08.Page_PreRender

09.Page_SaveStateComplete

10.Page_Unload

不合的处所在于:回发事故Button1_Click在Page_Load后被触发.

着末再把HttpApplication的事故斟酌进来,看下叠加后的顺序,不过先别发急,我们先来看一种特殊环境,假如一个asp.net利用根目录下未设置默认页,这时直接浏览根目录,比如http://localhost:2345/ 时,Globl.ascx.cs中定义的Application_XXX措施的调用顺序如下:

2010-03-28 15:01:39 413 Application_Start

2010-03-28 15:01:39 491 Init

2010-03-28 15:01:39 491 Application_BeginRequest

2010-03-28 15:01:39 506 Application_AuthenticateRequest

2010-03-28 15:01:39 506 Application_PostAuthenticateRequest

2010-03-28 15:01:39 506 Application_AuthorizeRequest

2010-03-28 15:01:39 522 Application_PostAuthorizeRequest

2010-03-28 15:01:39 522 Application_ResolveRequestCache

2010-03-28 15:01:39 522 Application_PostResolveRequestCache

2010-03-28 15:01:39 522 Application_PostMapRequestHandler

2010-03-28 15:01:39 522 Application_AcquireRequestState

2010-03-28 15:01:39 537 Application_PostAcquireRequestState

2010-03-28 15:01:39 537 Application_PreRequestHandlerExecute

2010-03-28 15:01:39 553 Application_Error

2010-03-28 15:01:39 553 Application_EndRequest

2010-03-28 15:01:39 569 Application_PreSendRequestHeaders

2010-03-28 15:01:39 569 Application_PreSendRequestContent

可以看到会触发Application_Error事故,即HttpRuntime觉得这是一个差错.

紧接着再浏览一个实际存在的页面,假如这时利用法度榜样有严重差错,导致Application关闭(比如web.config设置设置设备摆设摆设差错),调用的顺序如下:

2010-03-28 15:03:47 704 Application_BeginRequest

2010-03-28 15:03:47 704 Application_AuthenticateRequest

2010-03-28 15:03:47 766 Application_PostAuthenticateRequest

2010-03-28 15:03:47 766 Application_AuthorizeRequest

2010-03-28 15:03:47 766 Application_PostAuthorizeRequest

2010-03-28 15:03:47 766 Application_ResolveRequestCache

2010-03-28 15:03:47 783 Application_PostResolveRequestCache

2010-03-28 15:03:48 667 Application_PostMapRequestHandler

2010-03-28 15:03:48 667 Application_AcquireRequestState

2010-03-28 15:03:48 683 Application_PostAcquireRequestState

2010-03-28 15:03:48 698 Application_PreRequestHandlerExecute

2010-03-28 15:03:48 745 Page_PreInit

2010-03-28 15:04:02 903 Page_Unload

2010-03-28 15:04:02 903 Application_Error

2010-03-28 15:04:02 918 Application_EndRequest

2010-03-28 15:04:02 996 Application_PreSendRequestHeaders

2010-03-28 15:04:02 996 Application_PreSendRequestContent

2010-03-28 15:04:03 371 Application_Disposed

2010-03-28 15:04:03 371 Dispose

2010-03-28 15:04:03 386 Application_End

比较刚才的顺序,会发明Application_Start及Init没有再次被调用,也印证了文章前面提到的一些结论(Application_Start在全部asp.net利用生命周期内只触发一次),而且从着末的三个输出能知道:利用法度榜样关闭时Application_Disposed,Dispose,Application_End按顺序调用.

再"从新"浏览(指web Server重启)一下正常造访的页面,在不掉足也不回发的环境下,顺序如下:

2010-03-28 15:08:11 513 Application_Start

2010-03-28 15:08:11 591 Init

2010-03-28 15:08:11 591 Application_BeginRequest

2010-03-28 15:08:11 591 Application_AuthenticateRequest

2010-03-28 15:08:11 591 Application_PostAuthenticateRequest

2010-03-28 15:08:11 606 Application_AuthorizeRequest

2010-03-28 15:08:11 606 Application_PostAuthorizeRequest

2010-03-28 15:08:11 606 Application_ResolveRequestCache

2010-03-28 15:08:11 606 Application_PostResolveRequestCache

2010-03-28 15:08:11 622 Application_PostMapRequestHandler

2010-03-28 15:08:11 637 Application_EndRequest

2010-03-28 15:08:11 637 Application_PreSendRequestHeaders

2010-03-28 15:08:11 637 Application_PreSendRequestContent

2010-03-28 15:08:11 637 Application_BeginRequest

2010-03-28 15:08:11 637 Application_AuthenticateRequest

2010-03-28 15:08:11 653 Application_PostAuthenticateRequest

2010-03-28 15:08:11 653 Application_AuthorizeRequest

2010-03-28 15:08:11 653 Application_PostAuthorizeRequest

2010-03-28 15:08:11 653 Application_ResolveRequestCache

2010-03-28 15:08:11 653 Application_PostResolveRequestCache

2010-03-28 15:08:11 653 Application_PostMapRequestHandler

2010-03-28 15:08:11 653 Session_Start

2010-03-28 15:08:11 653 Application_AcquireRequestState

2010-03-28 15:08:11 653 Application_PostAcquireRequestState

2010-03-28 15:08:11 653 Application_PreRequestHandlerExecute

2010-03-28 15:08:11 669 Page_PreInit

2010-03-28 15:08:11 684 Page_Init

2010-03-28 15:08:11 684 Page_InitComplete

2010-03-28 15:08:11 684 Page_PreLoad

2010-03-28 15:08:11 684 Page_Load

2010-03-28 15:08:11 684 Page_LoadComplete

2010-03-28 15:08:11 684 Page_PreRender

2010-03-28 15:08:11 684 Page_SaveStateComplete

2010-03-28 15:08:11 700 Page_Unload

2010-03-28 15:08:11 700 Application_PostRequestHandlerExecute

2010-03-28 15:08:11 700 Application_ReleaseRequestState

2010-03-28 15:08:11 700 Application_PostReleaseRequestState

2010-03-28 15:08:11 700 Application_UpdateRequestCache

2010-03-28 15:08:11 700 Application_PostUpdateRequestCache

2010-03-28 15:08:11 700 Application_EndRequest

2010-03-28 15:08:11 700 Application_PreSendRequestHeaders

2010-03-28 15:08:11 700 Application_PreSendRequestContent

2010-03-28 15:08:11 793 Application_BeginRequest

2010-03-28 15:08:11 793 Application_AuthenticateRequest

2010-03-28 15:08:11 793 Application_PostAuthenticateRequest

2010-03-28 15:08:11 793 Application_AuthorizeRequest

2010-03-28 15:08:11 793 Application_PostAuthorizeRequest

2010-03-28 15:08:11 793 Application_ResolveRequestCache

2010-03-28 15:08:11 793 Application_PostResolveRequestCache

2010-03-28 15:08:11 809 Application_PostMapRequestHandler

2010-03-28 15:08:11 809 Application_AcquireRequestState

2010-03-28 15:08:11 809 Application_PostAcquireRequestState

2010-03-28 15:08:11 809 Application_PreRequestHandlerExecute

2010-03-28 15:08:11 825 Application_PostRequestHandlerExecute

2010-03-28 15:08:11 825 Application_ReleaseRequestState

2010-03-28 15:08:11 840 Application_PostReleaseRequestState

2010-03-28 15:08:11 949 Application_UpdateRequestCache

2010-03-28 15:08:11 949 Application_PostUpdateRequestCache

2010-03-28 15:08:11 965 Application_EndRequest

2010-03-28 15:08:11 981 Application_PreSendRequestHeaders

2010-03-28 15:08:11 981 Application_PreSendRequestContent

哇!原本一个页面造访下来,会调用到这么多的措施,怪不得很多高并发的大年夜型网站,平日都要自己写一个精减的HttpHandler用来取代Page做为基类,以期望得到更好的机能

着末:我们在做网站开拓时,弗成能只用到Page页,很多时刻还会用到UserControl(用户自定义控件),先看下它的承袭关系,比如我们创建了一个TestUserControl的用户控件

TestUserControl --> UserControl ---> TemplateControl --> Control

终极在Control类的定义下,可以看到

这彷佛注解用户控件中,应该有Page_Init,Page_Load,Page_Unload...等事故,平日我们只用到Init,Load事故,假如加入一个用户控件后,全部生命周期就更繁杂了:

2010-06-12 15:35:28 042 Application_Start

2010-06-12 15:35:28 072 Init

2010-06-12 15:35:28 072 Application_BeginRequest

2010-06-12 15:35:28 082 Application_AuthenticateRequest

2010-06-12 15:35:28 082 Application_PostAuthenticateRequest

2010-06-12 15:35:28 092 Application_AuthorizeRequest

2010-06-12 15:35:28 102 Application_PostAuthorizeRequest

2010-06-12 15:35:28 102 Application_ResolveRequestCache

2010-06-12 15:35:28 112 Application_PostResolveRequestCache

2010-06-12 15:35:28 122 Application_PostMapRequestHandler

2010-06-12 15:35:28 142 Application_EndRequest

2010-06-12 15:35:28 142 Application_PreSendRequestHeaders

2010-06-12 15:35:28 142 Application_PreSendRequestContent

2010-06-12 15:35:28 152 Application_BeginRequest

2010-06-12 15:35:28 152 Application_AuthenticateRequest

2010-06-12 15:35:28 162 Application_PostAuthenticateRequest

2010-06-12 15:35:28 162 Application_AuthorizeRequest

2010-06-12 15:35:28 162 Application_PostAuthorizeRequest

2010-06-12 15:35:28 172 Application_ResolveRequestCache

2010-06-12 15:35:28 172 Application_PostResolveRequestCache

2010-06-12 15:35:28 172 Application_PostMapRequestHandler

2010-06-12 15:35:28 172 Session_Start

2010-06-12 15:35:28 172 Application_AcquireRequestState

2010-06-12 15:35:28 182 Application_PostAcquireRequestState

2010-06-12 15:35:28 182 Application_PreRequestHandlerExecute

2010-06-12 15:35:28 192 Page_PreInit

2010-06-12 15:35:28 192 TestUserControl.Page_Init

2010-06-12 15:35:28 202 Page_Init

2010-06-12 15:35:28 202 TestUserControl.TestProperty.Set

2010-06-12 15:35:28 202 Page_InitComplete

2010-06-12 15:35:28 202 Page_PreLoad

2010-06-12 15:35:28 202 Page_Load

2010-06-12 15:35:28 202 TestUserControl.Page_Load

2010-06-12 15:35:28 202 TestUserControl.ShowData()

2010-06-12 15:35:28 212 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 212 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 212 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 212 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 212 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 222 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 222 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 222 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 222 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 222 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 232 TestUserControl.Repeater1.ItemDataBound()

2010-06-12 15:35:28 232 Page_LoadComplete

2010-06-12 15:35:28 232 Page_PreRender

2010-06-12 15:35:28 232 TestUserControl.Page_PreRender

2010-06-12 15:35:28 242 Page_SaveStateComplete

2010-06-12 15:35:28 242 TestUserControl.Page_Unload

2010-06-12 15:35:28 252 Page_Unload

2010-06-12 15:35:28 252 Application_PostRequestHandlerExecute

2010-06-12 15:35:28 252 Application_ReleaseRequestState

2010-06-12 15:35:28 252 Application_PostReleaseRequestState

2010-06-12 15:35:28 262 Application_UpdateRequestCache

2010-06-12 15:35:28 262 Application_PostUpdateRequestCache

2010-06-12 15:35:28 262 Application_EndRequest

2010-06-12 15:35:28 272 Application_PreSendRequestHeaders

2010-06-12 15:35:28 272 Application_PreSendRequestContent

2010-06-12 15:35:28 282 Application_BeginRequest

2010-06-12 15:35:28 292 Application_AuthenticateRequest

2010-06-12 15:35:28 292 Application_PostAuthenticateRequest

2010-06-12 15:35:28 302 Application_AuthorizeRequest

2010-06-12 15:35:28 302 Application_PostAuthorizeRequest

2010-06-12 15:35:28 302 Application_ResolveRequestCache

2010-06-12 15:35:28 312 Application_PostResolveRequestCache

2010-06-12 15:35:28 312 Application_PostMapRequestHandler

2010-06-12 15:35:28 322 Application_AcquireRequestState

2010-06-12 15:35:28 322 Application_PostAcquireRequestState

2010-06-12 15:35:28 322 Application_PreRequestHandlerExecute

2010-06-12 15:35:28 332 Application_PostRequestHandlerExecute

2010-06-12 15:35:28 332 Application_ReleaseRequestState

2010-06-12 15:35:28 332 Application_PostReleaseRequestState

2010-06-12 15:35:28 342 Application_UpdateRequestCache

2010-06-12 15:35:28 342 Application_PostUpdateRequestCache

2010-06-12 15:35:28 342 Application_EndRequest

2010-06-12 15:35:28 342 Application_PreSendRequestHeaders

2010-06-12 15:35:28 342 Application_PreSendRequestContent

2010-06-12 15:36:40 034 Session_End

更准确的页面生命周期解释,请查阅下面的文档,这是msdn官方网站对付Asp.Net页面生命周期的势力巨子解释

Asp.Net-Page-Life.rar

文中测试所用的源代码: aspx_life_test.rar (用vs2010 RC打开)

转自:http://www.cnblogs.com/yjmyzz/archive/2010/03/28/1698968.html

您可能还会对下面的文章感兴趣: