---------- 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 開發模式的使用時機與技巧