Aspx 生命周期剖析

上篇说到aspx 和他的C#后台代码便宜为一个dll,PageHandlerFactory 要返回一个Handler,进去他的源码看到,最终它返回的是Page,这个没问题,Page实现了IhttpHandler.

我们常说的Aspx 的页面生命周期,是怎么实现的呢?

PageHandlerFactory  要返回一个IHTTPHandler , 这个handler要执行的是ProcessRequst(httpContxt),

进到这个方法,最后会找到一个函数,这个函数的全部实现如下:

补充:

 this.Trace.Write("aspx.page", "Begin PreInit");

这个中间的代码,就是我们的事件,所有的页面生命周期都在这里

this.Trace.Write("aspx.page", "End PreInit");


// 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();
                if (this._requestValueCollection !null)
                {
                    this._unvalidatedRequestValueCollection = this.PageAdapter.DeterminePostBackModeUnvalidated();
                }
            }
            else
            {
                this._requestValueCollection = this.DeterminePostBackMode();
                if (this._requestValueCollection !null)
                {
                    this._unvalidatedRequestValueCollection = this.DeterminePostBackModeUnvalidated();
                }
            }
            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))
                {
                    this._pageFlags.Set(4);
                }
                string text3 = this.Request.QueryString["query"];
                if (text3 == null)
                {
                    text3 = string.Empty;
                }
                this.Request.QueryStringText = text3;
                context.Trace.IsEnabled = false;
            }
            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"], Purpose.WebForms_Page_PreviousPageID));
                            }
                            catch
                            {
                                this._pageFlags[8] = true;
                            }
                            if (virtualPath !null && virtualPath !this.Request.CurrentExecutionFilePathObject)
                            {
                                this._pageFlags[8] = true;
                                this._previousPagePath = virtualPath;
                            }
                        }
                    }
                }
            }
            if (this.MaintainScrollPositionOnPostBack)
            {
                this.LoadScrollPosition();
            }
            if (context.TraceIsEnabled)
            {
                this.Trace.Write("aspx.page", "Begin PreInit");
            }
            if (EtwTrace.IsTraceEnabled(5, 4))
            {
                EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_ENTER, this._context.WorkerRequest);
            }
            this.PerformPreInit();
            if (EtwTrace.IsTraceEnabled(5, 4))
            {
                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");
            }
            if (EtwTrace.IsTraceEnabled(5, 4))
            {
                EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_ENTER, this._context.WorkerRequest);
            }
            this.InitRecursive(null);
            if (EtwTrace.IsTraceEnabled(5, 4))
            {
                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");
            }
            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");
                }
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_ENTER, this._context.WorkerRequest);
                }
                this.LoadAllState();
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    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");
                }
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_ENTER, this._context.WorkerRequest);
                }
                this.ProcessPostData(this._requestValueCollection, true);
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    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");
            }
            this.OnPreLoad(EventArgs.Empty);
            if (context.TraceIsEnabled)
            {
                this.Trace.Write("aspx.page", "End PreLoad");
            }
            if (context.TraceIsEnabled)
            {
                this.Trace.Write("aspx.page", "Begin Load");
            }
            if (EtwTrace.IsTraceEnabled(5, 4))
            {
                EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_ENTER, this._context.WorkerRequest);
            }
            this.LoadRecursive();
            if (EtwTrace.IsTraceEnabled(5, 4))
            {
                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");
                }
                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");
                }
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_ENTER, this._context.WorkerRequest);
                }
                this.RaiseChangedEvents();
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    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");
                }
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_ENTER, this._context.WorkerRequest);
                }
                this.RaisePostBackEvent(this._requestValueCollection);
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    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");
            }
            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");
                    }
                    if (EtwTrace.IsTraceEnabled(5, 4))
                    {
                        EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_ENTER, this._context.WorkerRequest);
                    }
                    this.PreRenderRecursiveInternal();
                    if (EtwTrace.IsTraceEnabled(5, 4))
                    {
                        EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_LEAVE, this._context.WorkerRequest);
                    }
                    if (context.TraceIsEnabled)
                    {
                        this.Trace.Write("aspx.page", "End PreRender");
                    }
                }
            }
        }
        if (this._legacyAsyncInfo == null || this._legacyAsyncInfo.CallerIsBlocking)
        {
            this.ExecuteRegisteredAsyncTasks();
        }
        this.ValidateRawUrlIfRequired();
        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");
                    }
                    if (EtwTrace.IsTraceEnabled(5, 4))
                    {
                        EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER, this._context.WorkerRequest);
                    }
                    this.SaveAllState();
                    if (EtwTrace.IsTraceEnabled(5, 4))
                    {
                        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");
                    }
                    if (EtwTrace.IsTraceEnabled(5, 4))
                    {
                        EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_ENTER, this._context.WorkerRequest);
                    }
                    if (text !null)
                    {
                        this.ExportWebPart(text);
                    }
                    else
                    {
                        this.RenderControl(this.CreateHtmlTextWriter(this.Response.Output));
                    }
                    if (EtwTrace.IsTraceEnabled(5, 4))
                    {
                        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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值