依照觸發順序排列: Application_Init :在應用程序被實例化或第一次被調用時,該事件被觸發。對於所有的HttpApplication 對象實例,它都會被調用。 Application_Start :在HttpApplication 類的第一個實例被創建時,該事件被觸發。它允許你創建可以由所有HttpApplication 實例訪問的對象。 Session_Start :在一個新用戶訪問應用程序 Web 站點時,該事件被觸發。 Application_BeginRequest :在接收到一個應用程序請求時觸發。對於一個請求來說,它是第一個被觸發的事件,請求一般是用戶輸入的一個頁面請求(URL)。 Application_AuthenticateRequest :在安全模塊建立起當前用戶的有效的身份時,該事件被觸發。在這個時候,用戶的憑據將會被驗證。 Application_AuthorizeRequest :當安全模塊確認一個用戶可以訪問資源之後,該事件被觸發。 Application_ResolveRequestCache :在 ASP.NET 頁面框架完成一個授權請求時,該事件被觸發。它允許緩存模塊從緩存中爲請求提供服務,從而繞過事件處理程序的執行。 Application_AcquireRequestState :在 ASP.NET 頁面框架得到與當前請求相關的當前狀態(Session 狀態)時,該事件被觸發。 Application_PreRequestHandlerExecute :在 ASP.NET 頁面框架開始執行諸如頁面或 Web 服務之類的事件處理程序之前,該事件被觸發。 Application_PreSendRequestHeaders :在 ASP.NET 頁面框架發送 HTTP 頭給請求客戶(瀏覽器)時,該事件被觸發。 Application_PreSendRequestContent :待確認是Application_PreSendRequestContent 還是 Application_PreSendContent ? Application_PreSendContent :在 ASP.NET 頁面框架發送內容給請求客戶(瀏覽器)時,該事件被觸發。 Application_PostRequestHandlerExecute:在 ASP.NET 頁面框架結束執行一個事件處理程序時,該事件被觸發。 Application_ReleaseRequestState :在 ASP.NET 頁面框架執行完所有的事件處理程序時,該事件被觸發。這將導致所有的狀態模塊保存它們當前的狀態數據。 Application_UpdateRequestCache :在 ASP.NET 頁面框架完成事件處理程序的執行時,該事件被觸發,從而使緩存模塊存儲響應數據,以供響應後續的請求時使用。 Session_End :在一個用戶的會話超時、結束或他們離開應用程序 Web 站點時,該事件被觸發。 Application_EndRequest :針對應用程序請求的最後一個事件。 Application_End :在HttpApplication 類的最後一個實例被銷燬時,該事件被觸發。在一個應用程序的生命週期內它只被觸發一次。 Application_Disposed :在應用程序被銷燬之前觸發。這是清除以前所用資源的理想位置。 Application_Error :當應用程序中遇到一個未處理的異常時,該事件被觸發。 Page Event Flow: PreInit事件 Init事件 InitComplete事件 PreLoad事件 Load事件 LoadComplete事件 PreRender事件 PreRenderComplete事件 UnLoad事件 ---------- 20190211 ref: https://stackoverflow.com/questions/2340572/what-is-the-purpose-of-global-asax-in-asp-net Global asax events explained Application_Init: Fired when an application initializes or is first called. It's invoked for all HttpApplication object instances. Application_Disposed: Fired just before an application is destroyed. This is the ideal location for cleaning up previously used resources. Application_Error: Fired when an unhandled exception is encountered within the application. Application_Start: Fired when the first instance of the HttpApplication class is created. It allows you to create objects that are accessible by all HttpApplication instances. Application_End: Fired when the last instance of an HttpApplication class is destroyed. It's fired only once during an application's lifetime. Application_BeginRequest: Fired when an application request is received. It's the first event fired for a request, which is often a page request (URL) that a user enters. Application_EndRequest: The last event fired for an application request. Application_PreRequestHandlerExecute: Fired before the ASP.NET page framework begins executing an event handler like a page or Web service. Application_PostRequestHandlerExecute: Fired when the ASP.NET page framework is finished executing an event handler. Applcation_PreSendRequestHeaders: Fired before the ASP.NET page framework sends HTTP headers to a requesting client (browser). Application_PreSendContent: Fired before the ASP.NET page framework sends content to a requesting client (browser). Application_AcquireRequestState: Fired when the ASP.NET page framework gets the current state (Session state) related to the current request. Application_ReleaseRequestState: Fired when the ASP.NET page framework completes execution of all event handlers. This results in all state modules to save their current state data. Application_ResolveRequestCache: Fired when the ASP.NET page framework completes an authorization request. It allows caching modules to serve the request from the cache, thus bypassing handler execution. Application_UpdateRequestCache: Fired when the ASP.NET page framework completes handler execution to allow caching modules to store responses to be used to handle subsequent requests. Application_AuthenticateRequest: Fired when the security module has established the current user's identity as valid. At this point, the user's credentials have been validated. Application_AuthorizeRequest: Fired when the security module has verified that a user can access resources. Session_Start: Fired when a new user visits the application Web site. Session_End: Fired when a user's session times out, ends, or they leave the application Web site. ---------- 20190211 ref: http://paladinprogram.blogspot.com/2011/05/globalasax-application.html Gloabl.asax 的 Application 的事件觸發順序: Application_BeginRequest Application_AuthenticateRequest Application_AuthorizeRequest Application_ResolveRequestCache Application_AcquireRequestState Application_PreRequestHandllerExecute HTTP Handler Application_PostRequestHandlerExecute Application_ReleaseRequestState Application_UpdateRequestCache Application_EndRequest 在 Global.asax 所定義的事件,依據 ASP.NET Application Events in Global.asax 分類,可以分為:每次頁面的請求都會觸發,與依據條件來觸發兩種。在上圖所表示的,則是每個被使用者要求的頁面都會觸發的事件順序。 舉例來說,如果在 Application_BeginRequest 事件寫一行 Response.Write("paladin"); 則這個網站的每個頁面開頭第一行都會秀出 paladin 這個字眼。 另外依據條件來觸發的事件,則是如下: Application_Start Session_Start Application_Error Session_End Application_End Application_Disposed 從 Visual Studio 2005 開始,預設已經在方案總管理看不到 Global.asax 這檔案了。但如果個人有這方面的需求,還是可以透過新增的方式把這檔案加入。當你打開這 Global.asax 檔案時,會發現他是屬於 Code Inline 的寫法,對於習慣 Code Behind 的人來說,會有一點點不同,詳細的比較可以參考保哥文章(關於 Code Behind 與 Code Inline 開發模式的使用時機與技巧)。假如你想在 Global.asax 撰寫 Application_BeginRequest 事件,則只要直接加入下面一段就可以: void Application_BeginRequest(object sender, EventArgs e) { //撰寫自己的 BeginRequest 程式碼 Response.Write("***************"); } Steven Cheng 曾在論壇中提過另外有關 Code Behind 的寫法。 1.在與 Global.asax 同一層的檔案結構,新增一個 globalcode.cs 檔案。並讓這個類別繼承 HttpApplication。 //globalcode.cs public partial class globalcode : HttpApplication { public globalcode() { } void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.Write("paladin is here.."); } } 2.將原先 Global.asax 裡的 <%@ Application Language="C#" %> 修改成 <%@ Application Language="C#" CodeFile="globalcode.cs" Inherits="globalcode" %> 如此,也可以讓 Global.asax 以 Code Behind 來撰寫了。 參考: 01:ASP.NET Application Events in Global.asax 02:如何:使用 Visual C# .NET 建立 ASP.NET HTTP 模組 03:Working with the ASP.NET Global.asax file 04:Application_BeginRequest does not seem to run 05:關於 Code Behind 與 Code Inline 開發模式的使用時機與技巧