ASPNET应用程序与页面生命周期DOC_第1页
ASPNET应用程序与页面生命周期DOC_第2页
ASPNET应用程序与页面生命周期DOC_第3页
ASPNET应用程序与页面生命周期DOC_第4页
ASPNET应用程序与页面生命周期DOC_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、ASP. NET 应用程序与页面生命周期在本文中,我们将了解不同的事件,ASP.NET 应用程序的生命周期以浏览器向Web服务器(对于 ASP.NET 应用程序,通常为 IIS )发送请求为起点,直至将请求结果返回至浏览 器结束。在这个过程中,首先我们需要了解 ASP.NET请求的2个大致的步骤。其次我们将详细 了解httphandler ,httpmodule 和 页面对象(Page)中不同的事件的执行顺序,逻辑。二个步骤的过程:请求处理,2步的过程如下所示,用户发送一个请求到IIS 服务器:1、创建一个运行时,可以处理请求。换句话说,它创建应用程序对象,请求,响应和上下文对象处理请求。2、

2、运行时一旦被创建,请求处理,通过一系列的事件处理模块,Han dler处理和页面对象。简称 MHPM (Module, han dler, page and Module eve nt)。Create ASP.NETA z严serving the requestProcess request using module handler and ASP.NET page eventsASP.NET 应用程序生命周期的各个阶段:步骤1 :用户从Web 服务器请求应用程序资源 ASP.NET应用程序的生命周期以浏览器向Web服务器(对于 ASP.NET 应用程序,通常为 IIS )发送请求为起点。AS

3、P.NET 是 Web服务器下的ISAPI扩展。Web服务器接收到请求时,会对所请求的文件的文件扩展名进行检 查,确定应由哪个ISAPI扩展处理该请求,然后将该请求传递给合适的ISAPI扩展ASP.NET处理已映射到其上的文件扩展名,如.aspx、.ascx、.ashx 和.asmx 。步骤2 : ASP.NET接收对应用程序的第一个请求当ASP.NET接收到对应用程序中任何资源的第一个请求时,名为Applicatio nMan ager的类会创建一个应用程序域。应用程序域为全局变量提供应用程序隔离,并允许单独卸载每个应用程序。在应用程序域中,将为名为HostingEnvironment的类创

4、建一个实例,该实例提供对有关应用程序的信息 (如存储该应用程序的文件夹的名称)的访问。 需严端Internet 圏:应用程序硝 IIS :威扎他Web雄齐加16ASP.NET 的 12再A 口 pliicationManager宿屯耳境步骤3 :为每个请求创建ASP.NET 核心对象创建了应用程序域并对 HostingEnvironment对象进行了实例化之后,ASP.NET 将创建并初始化核心对象,如HttpCo ntext 类包含特定于当前应用程序对象。HttpRequest 对象包含有关当前请HttpContext 、HttpRequest 和 HttpResponse 请求的对象,如

5、HttpRequest 和 HttpResponse求的信息,包括 Cookie 和浏览器信息。HttpRespo nse对象包含发送到客户端的响应,包括所有呈现的输岀和Cookie步骤4 :将HttpApplication对象分配给请求 初始化所有核心应用程序对象之后,将通过创建HttpApplication类的实例启动应用程序。如果应用程序具有Global.asax 文件,则ASP.NET 会创建 Global.asax 类(从HttpApplication类派生)的一个实例,并使用该派生类表示应用程序。注:第一次在应用程序中请求ASP.NET页或进程时,将创建 HttpApplicati

6、on的一个新实例。不过,为了尽可能提高性能,可对多个请求重复使用HttpApplicatio n实例。Internee图:应用程序环境Af*iSASP.NETHttpRuntimeHttpContextHttpRequesiHttpResponseHttpApplicationIIS Vitfe web lit; 器)ASP.NET ISAP1HttpContext 刃HttpContext 己)HttpRequestHttpRequestHupResponseHttpReponseHttpApplicationHttpAppiicarion步骤 5 : 由 HttpApplication管线

7、处理请求 MHPM (Module, handler, page and Moduleeve nt)。g MttpApplicdtion 黄由 XttpApplicohon tM处空鮮.Appli6n対債分5asp*t權心曲ASP,NET Environment使用 MHPM (Module, han dler, page and Module eve nt)事件处理请求一旦HttpApplication对象创建,在处理该请求时将由HttpApplication 类执行。我们来了解下 HttpModule HttpHandlers。1、如果你想通过*.aspx ,*.html 文件来处理程序逻

8、辑,那么你需要使用HttpHandler,换句话说,httpha ndler是一个扩展的处理器。CicnrASPX requestvrww 斥2、如果你想使用 ASP.NET管道来处理程序逻辑,你需要使用httpmodule是一个事件处理器。HttpModule换句话说,BcyinReqirt 計AufhflrizeRequcstPoHondterEicec UteAuthenticdtefteuestPrtReue st HondterEc uteEndReestRe/uest Pipe Line如何处理请求。mhpm 有4个重要的步骤:步骤1 ( M: HttpModule ):客户端开始

9、请求处理。在引擎和httpmodule 中的事件可以用来处理用户自己定义的逻辑。有6个重要的事件,你可以在你的页面对象被创建之前调用 Beg in Request,authe nticaterequest,authorizerequest ,resolverequestcache ,acquirerequeststate 与 prerequesthandlerexecute。步骤2( H: HttpHandler ):旦上述6个事件被执行完, 引擎调用processrequest 事件,如果已实现httpha ndler在您的项目。步骤 3( P: ASP.NET page ):一旦 http

10、ha ndler 执行,asp. net 页面对象被创建。而 asp .net 页面对象被创建,Page对象的事件将被调用,这可以帮助我们页面中定制自定义逻辑的处理。有 6 个重要的事件,In it , Load, validate , eve nt, ren der ,unl oad 简称 SILVERS Start (does not signify anything as such just formsthe word) , I - (Init) , L (Load) , V (Validate), E (Event) and R (Render)。步骤4 ( M: HttpModule

11、 ): 一旦页面对象是执行和从内存中卸载,httpmodule提供网页执行事件可以用来注入自定义处理逻辑。有4个重要的后处理事件postrequesthandlerexecute,releaserequeststate ,updaterequestcache 与endrequest。Signifies that there 山 event with a 严 ad po$t.cr msftmce1 BcMquest *2 s t3 AuthorizeReque st *4 弭kefltque*池idu 5. Ac(uindlavesf5tdt4 *6 IVefteut stHandkr&rtcu

12、tt AuThtntkdttRtqucil has 阳c-AuThcntieatehequefr atd Ptat- Authentitc Request14. iPjstflequeHHondlerExeCuk 115 RekmtRquestSRk *16. UdateHcqutCbchc *!7, lEndftcqutst *Client7 Proceiiftequest *HJ-8. Init * 乳LwT10 Vdidatwn *11. Event * 亿 RtAder*13 Ufxwi-Rererber SILVER5 - StwiI -縮L -V - Validation E *

13、EventR 亠 renderU * UnloadM - HttpModule H HttpHandlerP - Asp.Net Page R - Response Final事件详细介绍:部分Event事件Description 描述HttpModuleBeg in Request在ASP.NET响应请求时作为 HTTP执行管线链中的第一个事件 发生。BeginRequest 事件发出信号表示创建任何给定的新请 求。此事件始终被引发,并且始终是请求处理期间发生的第一个 事件。HttpModuleAuthe nticateRequest当安全模块已建立用户标识时发生。AuthenticateR

14、equest事件发岀信号表示配置的身份验证机制已对当前请求进行了身份 验证。HttpModuleAuthorizeRequest安全模块已验证用户授权时发生。AuthorizeRequest事件发出信号表示ASP.NET已对当前请求进行了授权。HttpModuleResolveRequestCache在ASP.NET完成授权事件以使缓存模块从缓存中为请求提供服 务后发生,从而绕过事件处理程序(例如某个页或XML Webservices )的执行。HttpModuleAcquireRequestState当ASP.NET获取与当前请求关联的当前状态(如会话状态)时发生。AcquireReques

15、tState事件在创建了事件处理程序之后引发。HttpModulePreRequestHa ndlerExecute恰好在ASP.NET开始执行事件处理程序(例如,某页或某个XMLWeb services )前发生。HttpHa ndlerProcessRequestHttphandler 逻辑被执行。在本节中,我们会写,需要将每页扩展执行的逻辑。PageInitOnInit 方法执行创建 Page实例所需的初始化和设置步骤。 在页生命周期的此阶段中,页中声明的服务器控件都已初始化为 默认状态;但每个控件的视图状态尚未填充。在 Page_lnit 阶 段中,页中的控件不能访问该页的其他服务器控

16、件,无论其他控 件是子控件还是父控件。不一定会创建其他服务器控件,也不一 定能够访问它们PageLoadASP.NET控件完成加载,你写的UI操作逻辑或任何其他逻辑在这 里。PageValidate如果你有你的页面上有验证逻辑,这里检查是否符合验证。Render将页面最终的输岀发送到浏览器,如果你想做岀一些最终的 HTM 的变化,这是输出到浏览器,你可以在这里输入你的 HTML的逻 辑。PageUn load页面对象从内存中卸载。HttpModulePostRequestHa ndlerExecute在ASP.NET事件处理程序(例如,某页或某个XML Webservice ) 执行完毕时发生

17、。HttpModuleReleaserequestState在ASP.NET执行完所有请求事件处理程序后发生。该事件将使部分Event事件Description 描述状态模块保存当前状态数据。引发ReleaseRequestState事件以后,应用程序以该请求结束,并发出ASP.NET信号以存储该请求状态。HttpModuleUpdateRequestCacheBefore you end, if you want to update your cache.结束之前,如果您想更新您的高速缓存。HttpModuleEn dRequest当ASP.NET执行完事件处理程序以使缓存模块存储将用于从缓

18、 存为后续请求提供服务的响应时发生。演示代码:在这段代码中,我们创建HttpModule 和Httphandler添加所有请求响应的事件,下面用HttpModule Httpha ndler跟踪所有活动,并把它添加到一个全局性的集合变量中。1public class clsHttpModule : IHttpModule23privateHttpApplicati on httpApp;4publicstatic ArrayList objArrayList =n ewArrayList();5publicclsHttpModule()67891011publicvoid Dispose()1

19、213141516publicvoid In it(HttpApplicati on con text)1718this .httpApp = con text;19httpApp.C on text.Resp on se.Clear();20objArrayList.Clear();21objArrayList.Add(httpModule:l nit );22httpApp.Authe nticateRequest +=newEve ntHa ndler( On Authe nticatio n);23httpApp.AuthorizeRequest +=newEve ntHa ndler

20、( On Authorizatio n);new24 httpApp.Begi nRequest += Eve ntHa ndler( On Begi nrequest);n ewEve ntHa ndler( OnEn dRequest); new25 httpApp.E ndRequest +=26 httpApp.ResolveRequestCache += Eve ntHa ndler( On ResolveRequestCache);newnewnewnewnewsen der, Eve ntArgs a)27 httpApp.AcquireRequestState += Eve n

21、tHa ndler( On AcquireRequestState);28 httpApp.PreRequestHa ndlerExecute += Eve ntHa ndler( On PreRequestHa ndlerExecute);29 httpApp.PostRequestHa ndlerExecute += Eve ntHa ndler( On PostRequestHa ndlerExecute);30 httpApp.ReleaseRequestState += Eve ntHa ndler( On ReleaseRequestState);31 httpApp.Update

22、RequestCache += Eve ntHa ndler( On UpdateRequestCache);32void On UpdateRequestCache(object3334objArrayList.Add(httpModule:OnUpdateRequestCache);3536void OnReleaseRequestState( object sender, EventArgs a)3738objArrayList.Add(httpModule:O nReleaseRequestState);3940voidOnPostRequestHandlerExecute( obje

23、ct sender, EventArgs a)414243httpModule:O nPostRequestHa ndlerExecute);objArrayList.Add( void44OnPreRequestHandlerExecute( object sender, EventArgs a)454647httpModule:O nPreRequestHa ndlerExecute);objArrayList.Add(48voidOnAcquireRequestState( object sender, EventArgs a)4950httpModule: On AcquireRequ

24、estState);objArrayList.Add(5152void OnResolveRequestCache( object sender, EventArgs a)5354objArrayList.Add(httpModule:OnResolveRequestCache);5556void OnAuthorization(object sender, EventArgs a)5758596061626364656667686970717273747576777879808182objArrayList.Add(void On Authe nticati on(objArrayList.

25、Add(void On Beg in request(IIhttpModule: On Authorizatio n);object sender, EventArgs a)IIhttpModule:Authe nticateRequest);object sender, EventArgs a)objArrayList.Add(httpModule:Begi nRequest);void OnEndRequest(object sender, EventArgs a)objArrayList.Add(httpModule:EndRequest);objArrayList.Add();fore

26、ach ( string str in objArrayList)httpApp.Context.Response.Write(str +);以下代码片段是跟踪HttpHandler的ProcessRequest 事件:1 public class clsHttpHandler : IHttpHandler2 33 public bool IsReusable4 5 get return true ; 6 87 public void ProcessRequest(HttpC on text con text)8 11clsHttpModule.objArrayList.Add(HttpHa

27、ndler:ProcessRequest);12 con text.Resp on se.Redirect(/Default.aspx );13 14 Page事件public partial class _Default : System.Web.Ul.Page _protected void Page_init( objectsen der, Eve ntArgs e)clsHttpModule.objArrayList.Add(protected void Page_Load( objectclsHttpModule.objArrayList.Add(public override vo

28、id Validate。clsHttpModule.objArrayList.Add(Page:I nit);sen der, Eve ntArgs e)Page:Load);Page:Validate);页面123456789101112131415161718192021222324252627protected void Butt on 1_Click( object sender, EventArgs e)clsHttpModule.objArrayList.Add(Page:Event);protected override void Render(HtmlTextWriter ou

29、tput)clsHttpModule.objArrayList.Add( base.Re nder(output);Page:Render);protected void Page_Unload( objectsen der, Eve ntArgs e)clsHttpModule.objArrayList.Add(Page:U nLoad);28webc onfig中配置:HttpModules 配置 页面第一次加载效果:帝原型已1週童畑逊送择曰流程摸板设计器A远洋地产流保中心世外网待办列表e TabsButtonhttpXloduleilnithttpMo dule iB e ginRequ

30、e st http Mo dul e Authentic ateRe que st httpXIo chile: OnAnthorization httpModule: OnResoheReque stC ache httpMo dule: OnAc quireRequestState httpMo dule: OnPr eR.e questHandlerExecute Ps 呂 elnit Pageload Fag 亡 Render PagezUnLoad httpXIodulezOnPostRe questHandlerE xecute httpXlo dul e :OnReleas eR

31、e que stS tate httpModule: OnUpdateRe questC ache httpMo dule iEndRequest点击butt on 按钮后:httpModul e B eginReque st htipModul e Authentic ateRe que st httpXfodul e: OnAnthorization httpModul e:OnR esoheRe que stC ach e httpXfodul e:OnAc quireR e que stState httpXIodule: OnPre Re que stHandlerExe cute

32、Page:Init Page:LoadPage ventPageKender Page:UnLoad httpMo dul e: OnPo stRe que stHandlerEx e cute httpXlo dule: OnReleas eR e que stS tate httpXfodule:OnUpdaeRe questC ache httpXlo duleiEndRequestHttpModuleHttpModule 通过在某些事件中注册, 把自己插入 ASP.NET 请求处理管道。 当这些事件发生的 时候, ASP.NET 调用对相应的 HTTP 模块,这样该模块就能处理请求了。

33、常用操作:1 、向每个页面动态添加一些备注或说明性的文字:2 、判断用户登录如果定义了多个 HttpModule ,在 web.config 文件中引入自定义 HttpModule 的顺序就决定 了多个自定义 HttpModule 在处理一个 HTTP 请求的接管顺序。HttpHandlerHttpHandler 是 HTTP 请求的处理中心,真正地对客户端请求的服务器页面做出编译和执行, 并将处理过后的信息附加在 HTTP 请求信息流中再次返回到 HttpModule 中。HttpHandler 与 HttpModule 不同,一旦定义了自己的 HttpHandler 类,那么它对系统的Ht

34、tpHandler 的关系将是 “覆盖 ”关系。为了验证请求是否进入 HttpHandler ,我们可以在 WebConfig 中 加入:调试程序看看。综上我们可以总计出一张图:HtlMlEtcASP.NET 洌a*辛Hnpi:f=cMi=专 Mkus-AEsnEUflK2u?= 一HTtpjcodscA 三 hwr7exc4t=Hnpodu-f*xc f 二* CX2UC 共rr-二KA-Hfihndni土HUPM-ldup3clfIT?-K?lhiFV二kh*5M0duHL pdarrKCQWiSLu-chQ)HiTfLurt- End 兀 cquessi Hili 1i*HPMOJUrAcqlH

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论