ASP.NET中HttpApplication中ProcessRequest方法中執行的事件順序;ASP.NET WebForm和MVC總體請求流程圖...

weixin_30924079發表於2020-04-04

ASP.NET中HttpApplication中ProcessRequest方法中執行的事件順序

1.BeginRequest  開始處理請求

2.AuthenticateRequest 授權驗證請求,獲取使用者授權資訊
3.PostAuthenticateRequest 獲取成功
4.AunthorizeRequest 授權,一般來檢查使用者是否獲得許可權
5.PostAuthorizeRequest 獲得授權
6.ResolveRequestCache 獲取頁面快取結果(假設沒有則執行)
7.PostResolveRequestCache 已獲取快取
8.PostMapRequestHandler 建立頁面物件
9.AcquireRequestState 獲取Session -- 先推斷當前頁面物件是否實現了IRequiresSessionState介面,假設實現了,則從瀏覽器發來的請求報文頭中獲得SessionId,併到server的 Session池中獲得相應的Session物件,最後賦值給 HttpContext的Session屬性。
10.PostAcquireRequestState 獲得Session
11.PreRequestHandlerExecute 準備執行頁面物件
×執行頁面物件的ProcessRequest方法(1.ashx,1.aspx)。假設請求的是1.aspx。則會執行頁面生命週期
12.PostRequestHandlerExecute 執行完頁面物件了
13.ReleaseRequestState 釋放請求狀態
14.PostReleaseRequestState 已釋放請求狀態
15.UpdateReuqestCache 更新快取
16.PostUpdateRequestCache 已更新快取
17.LogRequest 日誌記錄
18.PostLogRequest 已完畢日誌

19.EndRequest 完畢


能夠再Global.asax中加入以Application_為字首+事件名 的方法執行測試!比如:

 public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);  
        }

    
        public void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication obj = sender as HttpApplication;
            obj.Context.Response.Write("1 Application_BeginRequest<br/>");
        }


        public void Application_ResolveRequestCache(object sender, EventArgs e)
        {
            HttpApplication obj = sender as HttpApplication;
            obj.Context.Response.Write("6 Application_ResolveRequestCache<br/>");
        }

        public void Application_PostResolveRequestCache(object sender, EventArgs e)
        {
            HttpApplication obj = sender as HttpApplication;
            obj.Context.Response.Write("7 Application_PostResolveRequestCache<br/>");
        }
        public void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication obj = sender as HttpApplication;
            obj.Context.Response.Write("11 Application_PreRequestHandlerExecute<br/>");
        }
    }
}


下面附上MVC總體請求流程圖。來自傳智播客黑馬培訓班


ASP.NET WebForm總體請求流程圖,來自傳智播客黑馬培訓班



轉載於:https://www.cnblogs.com/mqxnongmin/p/10490493.html

相關文章