ASP.NET 生命周期(原文翻译)

在网上看到这篇文章,老外写的,里面很多图片挺精致,顺带翻译过来给大家分享下,英语太次好多地方都翻不过来

ASP.NET application and page life cycle

Download source code - 4.03 KB

目录

  • 简介

  • 两步处理法
  • 创建ASP.NET环境
  • 使用MHPM事件处理请求
  • 在哪些事件里我们哪些事?
  • 一个简单的示范代码
  • 放大ASP.NET页面事件
  • 关于源码
  • references

简介

在这篇文章,我们将努力弄明白从用户发送请求开始到浏览器渲染完毕过程中所发生的不同事件。所以我们要搞清楚ASP.NET请求的两个大阶段,

然后我们来将进入从不同事件来触发‘HttpHandler’, ‘HttpModule’和ASP.NET页面对象。在这个事件旅程中,我们要理解每一个事件所发生逻辑过程

这个电子书适合涵盖.NET中的比如WCF,WPF,WWF,ajax,Core.NET,SQL等等知识点,你可以下载相同的内容from here 或者你可以追我每天的免费课程from here.

两步处理法

从30000英尺的高度来看(From 30,000 feet level,搞不清作者是不是在戏谑), ASP.NET 请求处理分两步进行,如下,用户发送一个请求到IIS:

  • ASP.NET创建一个可以处理该请求的环境,换句话说,它创建了应用程序对象,请求、形影和上下文对象来处理改请求
  • 当环境创建完毕,该请求被一系列经过一系列来自于使用模块(by using modules),处理器(handlers)和页面对象的事件进行处理,为了简化,我们把这一阶段命名为MHPM(Module,Handler,page and Module event),我们一会讲详细介绍

In the coming sections, we will understand both these main steps in more detail.

创建ASP.NET环境

Step 1: 用户发送了一个请求到IIS,IIS(进程inetinfo.exe)首先检查哪一个ISAPI可以处理这个请求。这个是由文件的扩展名来确定的,例如,如果页面是.aspx的页面,那么就会通过‘aspnet_isapi.dll’ 来处理

Step 2: 假如这是对这个站点发送的第一个请求,那么一个名字叫‘ApplicationManager’的类会创建一个应用程序域在站点运行的地方。众所周知,来自不同站点程序的应用程序域是在同一个IIS中是相互隔离的。所以如果在一个app域中存在问题,它是不会影响到其他的app域的。

Step 3: 新创建的app域创建托管环境(hosting environment),例如,‘HttpRuntime’对象。一旦托管环境被创建,那些必须的核心ASP.NET对象比如‘HttpContext’ , ‘HttpRequest’ and ‘HttpResponse’也会被创建.

Step 4: 一旦所有的ASP.NET核心对象被创建完毕,‘HttpApplication’对象被创建来服务用户的请求,如果你的系统中包含有一个‘global.asax’文件,那么‘global.asax’文件的对象会被创建。请记住global.asax文件时内置在‘HttpApplication’类中的。

Note: 第一次一个ASP.NET页面连接到一个app,一个新的‘HttpApplication’实例被创建,为了最大限度提高性能,‘HttpApplication’的实例可以重复用于多个请求

Step 5: 然后HttpApplication对象被分配到ASP.NET核心对象来处理页面.

Step 6: HttpApplication 然后通过HTTP模块事件,处理器和页面事件来处理请求。它会触发MHPM事件来进行请求处理

Note: 想了解过多, read this.

下面的图片说明了那些内部对象模块和ASP.NET请求的关系,在最顶层是创建‘Appdomain’的ASP.NET运行时,

Appdomain’包含‘request’, ‘response’ and ‘context’ 对象的‘HttpRuntime

使用MHPM事件处理请求

一旦‘HttpApplication’创建完毕,它开始处理请求了。它经历了三个不同的阶段.它在通过这些阶段,它调用不同的事件,在这些事件中,开发者可以扩展代码添加自己的逻辑到里面。

在我们继续之前,让我们搞清楚什么是‘HttpModule’ 和 ‘HttpHandlers’。他们帮助我们注入自定义逻辑在ASP.NET页面处理之前和之后,二者的主要区别如下:

· If you want to inject logic based in file extensions like ‘.ASPX’, ‘.HTML’, then you use ‘HttpHandler’. In other words, ‘HttpHandler’ is an extension based processor.

· If you want to inject logic in the events of ASP.NET pipleline, then you use ‘HttpModule’. ASP.NET. In other words, ‘HttpModule’ is an event based processor.

如果想了解更多,戳 here.
下面是请求处理的逻辑过程,下面展示了4个重要步骤:
Step 1(M: HttpModule): 客户请求处理开始,在ASP.NET引擎运行和创建前,ASP.NET HttpModule 提供可以注入自定义逻辑的事件,它提供了6个重要的事件你可以用在页面对象创建BeginRequest, AuthenticateRequest,AuthorizeRequest, ResolveRequestCache, AcquireRequestStatePreRequestHandlerExecute之前。

Step 2 (H: ‘HttpHandler’): 当上面6个事件执行完毕,ASP.NET引擎接着触发事件,假如你的项目中有执行HttpHandler ,ASP.NET引擎将会调用ProcessRequest事件。

Step 3 (P: ASP.NET page):HttpHandler 的逻辑执行,当前ASP.NET页面对象就创建完毕。当ASP.NET页面对象创建完,

一些事件被触发,我们可以在这些页面事件里编写自定义的逻辑,在ASP.NET里提供6个重要的事件我们可以编写自定义的逻辑。

页面的Init, Load, validate, event, render and unload.你可以用单词SILVER 来记忆这些事件,S-开始(在这个单词中没有特殊的含义),I-Init,L-Load,V-Validate,E-Event,R-Render

Step4 (M: HttpModule): 当页面对象执行完毕并从内存中卸载,HttpModule 提供了后期页面处理事件我们可以用来处理页面逻辑。

它有4个重要的后期处理事件, PostRequestHandlerExecute, ReleaserequestState, UpdateRequestCacheEndRequest.

下面的图片诠释了刚才的过程.

在哪些事件里我们哪些事?

价值百万美元的问题来了,在某个事件里,我们该做什么?下面的表格列出了在什么事件执行哪些逻辑处理的代码。


Section


Event


Description


HttpModule


BeginRequest


This event signals a new request; it is guaranteed to be raised on each request.


HttpModule


AuthenticateRequest


This event signals that ASP.NET runtime is ready to authenticate the user. Any authentication code can be injected here.


HttpModule


AuthorizeRequest


This event signals that ASP.NET runtime is ready to authorize the user. Any authorization code can be injected here.


HttpModule


ResolveRequestCache


In ASP.NET, we normally use outputcache directive to do caching. In this event, ASP.NET runtime determines if the page can be served from the cache rather than loading the patch from scratch. Any caching specific activity can be injected here.


HttpModule


AcquireRequestState


This event signals that ASP.NET runtime is ready to acquire session variables. Any processing you would like to do on session variables.


HttpModule


PreRequestHandlerExecute


This event is raised just prior to handling control to the HttpHandler. Before you want the control to be handed over to the handler any pre-processing you would like to do.


HttpHandler


ProcessRequest


Httphandler logic is executed. In this section, we will write logic which needs to be executed as per page extensions.


Page


Init


This event happens in the ASP.NET page and can be used for:

· Creating controls dynamically, in case you have controls to be created on runtime.

· Any setting initialization.

· Master pages and the settings.

In this section, we do not have access to viewstate, postedvalues and neither the controls are initialized.


Page


Load


In this section, the ASP.NET controls are fully loaded and you write UI manipulation logic or any other logic over here.


Page


Validate


If you have valuators on your page, you would like to check the same here.

 
Render


It’s now time to send the output to the browser. If you would like to make some changes to the final HTML which is going out to the browser, you can enter your HTML logic here.


Page


Unload


Page object is unloaded from the memory.


HttpModule


PostRequestHandlerExecute


Any logic you would like to inject after the handlers are executed.


HttpModule


ReleaserequestState


If you would like to save update some state variables like session variables.


HttpModule


UpdateRequestCache


Before you end, if you want to update your cache.


HttpModule


EndRequest


This is the last stage before your output is sent to the client browser.

一个简单的示范代码

With this article, we have attached a sample code which shows how the events actually fire. In this code, we have created a ‘HttpModule’ and ‘Httphandler’ in this project and we have displayed a simple response write in all events, below is how the output looks like.
Below is the class for ‘HttpModule’ which tracks all events and adds it to a global collection.

public class clsHttpModule : IHttpModule

{

......

void OnUpdateRequestCache(object sender, EventArgs a)

{

objArrayList.Add("httpModule:OnUpdateRequestCache");

}

void OnReleaseRequestState(object sender, EventArgs a)

{

objArrayList.Add("httpModule:OnReleaseRequestState");

}

void OnPostRequestHandlerExecute(object sender, EventArgs a)

{

objArrayList.Add("httpModule:OnPostRequestHandlerExecute");

}

void OnPreRequestHandlerExecute(object sender, EventArgs a)

{

objArrayList.Add("httpModule:OnPreRequestHandlerExecute");

}

void OnAcquireRequestState(object sender, EventArgs a)

{

objArrayList.Add("httpModule:OnAcquireRequestState");

}

void OnResolveRequestCache(object sender, EventArgs a)

{

objArrayList.Add("httpModule:OnResolveRequestCache");

}

void OnAuthorization(object sender, EventArgs a)

{

objArrayList.Add("httpModule:OnAuthorization");

}

void OnAuthentication(object sender, EventArgs a)

{

objArrayList.Add("httpModule:AuthenticateRequest");

}

void OnBeginrequest(object sender, EventArgs a)

{

objArrayList.Add("httpModule:BeginRequest");

}

void OnEndRequest(object sender, EventArgs a)

{

objArrayList.Add("httpModule:EndRequest");

objArrayList.Add("<hr>");

foreach (string str in objArrayList)

{

httpApp.Context.Response.Write(str + "<br>") ;

}

}

}

Below is the code snippet for ‘HttpHandler’ which tracks ‘ProcessRequest’ event.

public class clsHttpHandler : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

clsHttpModule.objArrayList.Add("HttpHandler:ProcessRequest");

context.Response.Redirect("Default.aspx");

}

}

We are also tracking all the events from the ASP.NET page.

public partial class _Default : System.Web.UI.Page

{

protected void Page_init(object sender, EventArgs e)

{

clsHttpModule.objArrayList.Add("Page:Init");

}

protected void Page_Load(object sender, EventArgs e)

{

clsHttpModule.objArrayList.Add("Page:Load");

}

public override void Validate()

{

clsHttpModule.objArrayList.Add("Page:Validate");

}

protected void Button1_Click(object sender, EventArgs e)

{

clsHttpModule.objArrayList.Add("Page:Event");

}

protected override void Render(HtmlTextWriter output)

{

clsHttpModule.objArrayList.Add("Page:Render");

base.Render(output);

}

protected void Page_Unload(object sender, EventArgs e)

{

clsHttpModule.objArrayList.Add("Page:UnLoad");

}}

Below is how the display looks like with all events as per the sequence discussed in the previous section.

放大ASP.NET页面事件

在上面的章节,我们浏览了一个ASP.NET页面请求的完整流程。其中最重要的是章节是ASP.NET页面,我们还没有讨论它的细节,So,让我们在这一章节花费一些精力来描述一下ASP.NET页面事件的更多细节。

一些ASP.NET有2部分,一部分是在浏览器上展示的带有HTML标签的部分,另一个存放在HTML input元素上的表单的viewstate隐藏域。当页面发布,在服务器端,带有viewstate和表单数据捆绑的ASP.NET控件创建了html标签, (原文:these HTML tags are created in to ASP.NET controls with viewstate and form data tied up together on the server,这句翻译起来好别扭,自己原文理解下吧),我们可以在后台代码中获取到所有的服务器端控件,可以执行和编写自定义的逻辑然后把页面渲染到浏览器

现在,对于这些作为ASP.NET控件的可连接服务器的HTML控件,ASP.NET提供了大量的可供我们自定义逻辑的事件。我们只需要把我们所需要定义的任务或逻辑放在合适的事件里面。(原文:Now between these HTML controls coming live on the server as ASP.NET controls, the ASP.NET page emits out lot of events which can be consumed to inject logic. Depending on what task / logic you want to perform, we need to put this logic appropriately in those events.这段翻译有问题。)
Note: 大多数开发者直接使用page_load处理所有的事情。这可不是一个好想法。So,它既要填充控件,设置view state,设置主题等等,每一件事情发生在page load。So,如果我们能把自定义逻辑按照事件自然属性的放入适当的事件中,那我们的代码看起来会更清爽些。


Seq


Events


Controls Initialized


View state
Available


Form data
Available


What Logic can be written here?


1


Init


No


No


No


Note: You can access form data etc. by using ASP.NET request objects but not by Server controls.Creating controls dynamically, in case you have controls to be created on runtime. Any settinginitialization.Master pages and them settings. In this section, we do not have access to viewstate , posted values and neither the controls are initialized.


2


Load view state


Not guaranteed


Yes


Not guaranteed


You can access view state and any synch logic where you want viewstate to be pushed to behind code variables can be done here.


3


PostBackdata


Not guaranteed


Yes


Yes


You can access form data. Any logic where you want the form data to be pushed to behind code variables can be done here.


4


Load


Yes


Yes


Yes


This is the place where you will put any logic you want to operate on the controls. Like flourishing a combobox from the database, sorting data on a grid, etc. In this event, we get access to all controls, viewstate and their posted values.


5


Validate


Yes


Yes


Yes


If your page has validators or you want to execute validation for your page, this is the right place to the same.


6


Event


Yes


Yes


Yes


If this is a post back by a button click or a dropdown change, then the relative events will be fired. Any kind of logic which is related to that event can be executed here.


7


Pre-render


Yes


Yes


Yes


If you want to make final changes to the UI objects like changing tree structure or property values, before these controls are saved in to view state.


8


Save view state


Yes


Yes


Yes


Once all changes to server controls are done, this event can be an opportunity to save control data in to view state.


9


Render


Yes


Yes


Yes


If you want to add some custom HTML to the output this is the place you can.


10


Unload


Yes


Yes


Yes


Any kind of clean up you would like to do here.

关于源码

This source code shows how the complete ASP.NET request cycle fires. You can download it from here.

References

I am not so smart to write this article by myself  , lot of things I have plugged from the below articles.

· Read more about IIS 7.0 life cycle http://msdn.microsoft.com/en-us/library/bb470252.aspx

· Intercepting filters http://msdn.microsoft.com/en-us/library/ms998536.aspx

· Explains how to implement Httphandlers and moduleshttp://msdn.microsoft.com/enus/library/system.web.httpapplication.aspx

· Httphandlers and Httpmodules :- http://www.15seconds.com/Issue/020417.htm

· Implementing security using modules and handlers http://joel.net/articles/asp.net2_security.aspx

· Difference between Httpapplication and global.asaxhttp://codebetter.com/blogs/karlseguin/archive/2006/06/12/146356.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

时间: 2024-10-16 12:43:14

ASP.NET 生命周期(原文翻译)的相关文章

Asp.net生命周期

asp.net生命周期一直就想对他彻底的动手搞清楚了.以前一直处于朦朦胧胧的认识状态,今天终于下狠心动手做了个例子来了解生命周期过程.之所以打算自己动手做是因为我一直觉得虽然网上有了类似的例子,不过只有自己亲手做一遍然后通过博客写出来才能使自己更深刻的理解这些内容.好了,不废话了,开始做. 编写代码 先来看一下整个项目的结构以便对我们做的例子有一个大体的了解.项目中我定义了一个module,handler,global和一个页面以及自定义的一个控件.因为有的过程中没法直接使用Response方法

ASP.NET生命周期详解 [转]

最近一直在学习ASP.NET MVC的生命周期,发现ASP.NET MVC是建立在ASP.NET Framework基础之上的,所以原来对于ASP.NET WebForm中的很多处理流程,如管道事件等,对于ASP.NET MVC同样适用.只是MVC URLRouting Module对进入到server的request进行了拦截,然后对此次request的handler进行了特殊的处理.总结来说,就是 ASP.NET管道是所有ASP.NET Web Applicaiton,包括WebForm,

Asp.Net生命周期和Http管道技术

本篇主要介绍一下内容: 1.ASP.NET生命周期 2.Http运行时 3.Http管道技术 a)inetinfo.exe b)asp.net_isapi.dll c)aspnet_wp.exe d)HttpHandler e)HttpModule 4.实现Httphandler的使用 5.ASP.NET生命周期 6.(IIS)Web服务器(inetinfo.exe): 1.只有少数几种被客户端请求的资源类型由iis直接处理,如对Html页面,文本文件,jpeg和gif图像的传入请求 2.对AS

Asp.Net生命周期系列一

Asp.Net生命周期对于初级甚至中级程序员来说,一直都是一个难题,很多程序员不了解生命周期,导致使用Asp.Net做开发感觉很不灵活,感觉太多东西被微软封装好了,我们不能改变,其实只要你稍微了解一下就知道,原来不是这样的! 我写这一系列文章是采用总分的方式,先让大家整体了解,然后再逐一突破.先将一个故事,也是园子里看到的(http://www.cnblogs.com/GodSpeed/archive/2010/06/19/1761095.html),我认为这个写的有些细节上的错误,稍稍添加些自

ASP.NET Page执行顺序(ASP.NET生命周期)

此部分说明的生命周期只有部分: ---引用MSDN 阶段 说明 页请求 页请求发生在页生命周期开始之前.用户请求页时,ASP.NET 将确定是否需要分析和编译页(从而开始页的生命周期),或者是否可以在不运行页的情况下发送页的缓存版本以进行响应. 开始 在开始阶段,将设置页属性,如 Request和 Response在此阶段,页还将确定请求是回发请求还是新请求,并设置 IsPostBack 属性.此外,在开始阶段期间,还将设置页的 UICulture 属性. 页初始化 页初始化期间,可以使用页中的

[译] ASP.NET 生命周期 – ASP.NET 上下文对象(五)

ASP.NET 上下文对象 ASP.NET 提供了一系列对象用来给当前请求,将要返回到客户端的响应,以及 Web 应用本身提供上下文信息.间接的,这些上下文对象也可以用来回去核心 ASP.NET 框架特性. 上下文对象提供了应用,当前请求,与当前请求相关联的响应的信息.也提供了对多数重要的 ASP.NET 平台服务的访问,比如安全与状态数据.我们可以在 MVC 框架的 controllers 和 views 中使用上下文对象来根据当前的请求或者应用状态数据来调整我们应用的响应.在创建模块或者处理

[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)

不使用特殊方法来处理请求生命周期事件 HttpApplication 类是全局应用类的基类,定义了可以直接使用的一般 C# 事件.那么使用标准 C# 事件还是特殊方法那就是个人偏好的问题了,如果喜欢,也可以将这两种方式混合起来使用. 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.W

[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(三)

使用特殊方法处理请求生命周期事件 为了在全局应用类中处理这些事件,我们会创建一个名称以 Application_ 开头,以事件名称结尾的方法,比如 Application_BeginRequest.举个例子,就像 Application_Start 和 Application_End 方法,ASP.NET 框架就会在事件触发的时候找到这些函数并触发它.下面是更新后的代码片段: 1 using System; 2 using System.Collections.Generic; 3 using

ASP.NET 生命周期 – ASP.NET 应用生命周期(一)

概述 ASP.NET 平台定义了两个非常重要的生命周期.第一个是 应用生命周期  (application life cycle),用来追踪应用从启动的那一刻到终止的那一刻.另一个就是 请求生命周期 (request life cycle),它定义了 HTTP 请求在 ASP.NET 平台中首次接收到,到最终响应发出之间的路径. ASP.NET 应用生命周期 在 ASP.NET 中有两个时刻——应用启动的时刻和应用停止接收请求的时刻,这两个时刻定义了应用生命周期.ASP.NET 在应用启动和当应