相关源码
页面的Process方法
// System.Web.UI.Page private void ProcessRequest(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) { if (includeStagesBeforeAsyncPoint) { //调用子类的方法(这是个虚方法),1,创建页面控件树 this.FrameworkInitialize(); base.ControlState = ControlState.FrameworkInitialized; } bool flag = this.Context.WorkerRequest is IIS7WorkerRequest; try { try { if (this.IsTransacted) { this.ProcessRequestTransacted(); } else { //页面声明周期的主要部分 this.ProcessRequestMain(includeStagesBeforeAsyncPoint, includeStagesAfterAsyncPoint); } if (includeStagesAfterAsyncPoint) { flag = false; this.ProcessRequestEndTrace(); } } catch (ThreadAbortException) { try { if (flag) { this.ProcessRequestEndTrace(); } } catch { } } finally { if (includeStagesAfterAsyncPoint) { this.ProcessRequestCleanup(); } } } catch { throw; } }
ProcessRequestMain方法
// System.Web.UI.Page private void ProcessRequestMain(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) { try { HttpContext context = this.Context; string text = null; if (includeStagesBeforeAsyncPoint) { if (this.IsInAspCompatMode) { AspCompatApplicationStep.OnPageStartSessionObjects(); } if (this.PageAdapter != null) { this._requestValueCollection = this.PageAdapter.DeterminePostBackMode(); } else { this._requestValueCollection = this.DeterminePostBackMode(); } string text2 = string.Empty; if (this.DetermineIsExportingWebPart()) { if (!RuntimeConfig.GetAppConfig().WebParts.EnableExport) { throw new InvalidOperationException(SR.GetString("WebPartExportHandler_DisabledExportHandler")); } text = this.Request.QueryString["webPart"]; if (string.IsNullOrEmpty(text)) { throw new InvalidOperationException(SR.GetString("WebPartExportHandler_InvalidArgument")); } if (string.Equals(this.Request.QueryString["scope"], "shared", StringComparison.OrdinalIgnoreCase)) { ); } string text3 = this.Request.QueryString["query"]; if (text3 == null) { text3 = string.Empty; } this.Request.QueryStringText = text3; context.Trace.IsEnabled = false; } //2,确定IsCallback值 if (this._requestValueCollection != null) { if (this._requestValueCollection["__VIEWSTATEENCRYPTED"] != null) { this.ContainsEncryptedViewState = true; } text2 = this._requestValueCollection["__CALLBACKID"]; if (text2 != null && this._request.HttpVerb == HttpVerb.POST) { this._isCallback = true; } else { if (!this.IsCrossPagePostBack) { VirtualPath virtualPath = null; if (this._requestValueCollection["__PREVIOUSPAGE"] != null) { try { virtualPath = VirtualPath.CreateNonRelativeAllowNull(Page.DecryptString(this._requestValueCollection["__PREVIOUSPAGE"])); } catch { ] = true; } if (virtualPath != null && virtualPath != this.Request.CurrentExecutionFilePathObject) { ] = true; this._previousPagePath = virtualPath; } } } } } if (this.MaintainScrollPositionOnPostBack) { this.LoadScrollPosition(); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "Begin PreInit"); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_ENTER, this._context.WorkerRequest); } //3.1初始化PreInit this.PerformPreInit(); , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_LEAVE, this._context.WorkerRequest); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End PreInit"); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "Begin Init"); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_ENTER, this._context.WorkerRequest); } //3.2递归初始化,因为要初始化树,所以用递归 this.InitRecursive(null); , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_LEAVE, this._context.WorkerRequest); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End Init"); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "Begin InitComplete"); } //3.3,初始化完成 this.OnInitComplete(EventArgs.Empty); if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End InitComplete"); } if (this.IsPostBack) { if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "Begin LoadState"); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_ENTER, this._context.WorkerRequest); } //4.1,加载ViewState this.LoadAllState(); , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_LEAVE, this._context.WorkerRequest); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End LoadState"); this.Trace.Write("aspx.page", "Begin ProcessPostData"); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_ENTER, this._context.WorkerRequest); } //4.2,处理回发数据 this.ProcessPostData(this._requestValueCollection, true); , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_LEAVE, this._context.WorkerRequest); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End ProcessPostData"); } } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "Begin PreLoad"); } //5.1页面预加载 this.OnPreLoad(EventArgs.Empty); if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End PreLoad"); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "Begin Load"); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_ENTER, this._context.WorkerRequest); } //5.2页面递归加载 this.LoadRecursive(); , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_LEAVE, this._context.WorkerRequest); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End Load"); } if (this.IsPostBack) { if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "Begin ProcessPostData Second Try"); } //5.3第二次尝试加载回发数据 this.ProcessPostData(this._leftoverPostData, false); if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End ProcessPostData Second Try"); this.Trace.Write("aspx.page", "Begin Raise ChangedEvents"); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_ENTER, this._context.WorkerRequest); } //6.1,触发Change事件 this.RaiseChangedEvents(); , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_LEAVE, this._context.WorkerRequest); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End Raise ChangedEvents"); this.Trace.Write("aspx.page", "Begin Raise PostBackEvent"); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_ENTER, this._context.WorkerRequest); } //6.2,触发点击事件 this.RaisePostBackEvent(this._requestValueCollection); , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_LEAVE, this._context.WorkerRequest); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End Raise PostBackEvent"); } } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "Begin LoadComplete"); } //5.3,页面加载完成 this.OnLoadComplete(EventArgs.Empty); if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End LoadComplete"); } if (this.IsPostBack && this.IsCallback) { this.PrepareCallback(text2); } else { if (!this.IsCrossPagePostBack) { if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "Begin PreRender"); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_ENTER, this._context.WorkerRequest); } //7,递归预渲染 this.PreRenderRecursiveInternal(); , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_LEAVE, this._context.WorkerRequest); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End PreRender"); } } } } if (this._asyncInfo == null || this._asyncInfo.CallerIsBlocking) { this.ExecuteRegisteredAsyncTasks(); } this._request.ValidateRawUrl(); if (includeStagesAfterAsyncPoint) { if (this.IsCallback) { this.RenderCallback(); } else { if (!this.IsCrossPagePostBack) { if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "Begin PreRenderComplete"); } this.PerformPreRenderComplete(); if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End PreRenderComplete"); } if (context.TraceIsEnabled) { this.BuildPageProfileTree(this.EnableViewState); this.Trace.Write("aspx.page", "Begin SaveState"); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER, this._context.WorkerRequest); } //8,保存页面状态到ViewState this.SaveAllState(); , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_LEAVE, this._context.WorkerRequest); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End SaveState"); this.Trace.Write("aspx.page", "Begin SaveStateComplete"); } this.OnSaveStateComplete(EventArgs.Empty); if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End SaveStateComplete"); this.Trace.Write("aspx.page", "Begin Render"); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_ENTER, this._context.WorkerRequest); } if (text != null) { this.ExportWebPart(text); } else { //9,渲染出HTML this.RenderControl(this.CreateHtmlTextWriter(this.Response.Output)); } , )) { EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_LEAVE, this._context.WorkerRequest); } if (context.TraceIsEnabled) { this.Trace.Write("aspx.page", "End Render"); } this.CheckRemainingAsyncTasks(false); } } } } catch (ThreadAbortException ex2) { HttpApplication.CancelModuleException ex = ex2.ExceptionState as HttpApplication.CancelModuleException; if (!includeStagesBeforeAsyncPoint || !includeStagesAfterAsyncPoint || this._context.Handler != this || this._context.ApplicationInstance == null || ex == null || ex.Timeout) { this.CheckRemainingAsyncTasks(true); throw; } this._context.ApplicationInstance.CompleteRequest(); Page.ThreadResetAbortWithAssert(); } catch (ConfigurationException) { throw; } catch (Exception e) { PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_DURING_REQUEST); PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_TOTAL); if (!this.HandleError(e)) { throw; } } }