1. 程式人生 > >如何確定asp.net請求生命周期的當前處理事件

如何確定asp.net請求生命周期的當前處理事件

authorize nbsp not text state uri public error security

1 首先在全局應用程序裏面添加如下代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace Events
{
    public class Global : System.Web.HttpApplication
    {

        public Global()
        {
            BeginRequest 
+= HandleEvent; EndRequest += HandleEvent; AcquireRequestState += HandleEvent; PostAcquireRequestState += HandleEvent; } private void HandleEvent(object sender, EventArgs e) { //httpContext類用於訪問所有有關應用程序,所處理的請求以及正在構建的請求的信息,並且它的屬性currentNotification定義了HttpApplication
事件的子集
string eventName = "<Unknown>"; switch (Context.CurrentNotification) { case RequestNotification.BeginRequest: case RequestNotification.EndRequest: eventName = Context.CurrentNotification.ToString();
break; case RequestNotification.AuthenticateRequest: break; case RequestNotification.AuthorizeRequest: break; case RequestNotification.ResolveRequestCache: break; case RequestNotification.MapRequestHandler: break; //當asp.net fromework需要與請求關聯的狀態時,將觸發改事件 case RequestNotification.AcquireRequestState: if (Context.IsPostNotification) { eventName = "PostAcquireRequestState"; } else { eventName = "AcquireRequestState"; } break; case RequestNotification.PreExecuteRequestHandler: break; case RequestNotification.ExecuteRequestHandler: break; case RequestNotification.ReleaseRequestState: break; case RequestNotification.UpdateRequestCache: break; case RequestNotification.LogRequest: break; case RequestNotification.SendResponse: break; default: break; } EventCollection.Add(EventSource.Application, eventName); } protected void Application_Start(object sender, EventArgs e) { EventCollection.Add(EventSource.Application, "Start"); Application["message"] = "Aplication Events"; } protected void Session_Start(object sender, EventArgs e) { } protected void Application_BeginRequest(object sender, EventArgs e) { //收到請求時觸發的第一個事件 EventCollection.Add(EventSource.Application, "BeginRequest"); Response.Write(string.Format("request started at {0}", DateTime.Now.ToLongTimeString())); } protected void Application_EndRequest(object sender,EventArgs e) { EventCollection.Add(EventSource.Application, "EndRequest"); } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { EventCollection.Add(EventSource.Application,"End"); } } }

如何確定asp.net請求生命周期的當前處理事件