解读System.Web.UI.Page中关键方法ProcessRequestMain()

为优化博客园程序性能,作者深入研究ASP.NET源代码。开发ASP.NET应用时,System.Web.UI.Page类常用但少有人研究其源码。作者花半天研究了Page类处理请求的关键方法ProcessRequestMain(),并分享自己的理解,去掉了代码中输出Trace信息部分。

     为了更好地优化博客园程序的性能,最近我在优化代码的同时,更深入地去研究asp.net的源代码。asp.net的源代码通过Reflector工具一鉴无遗, 虽然不是原版的代码,但已经足够了,其中的原理与思想已经清楚地摆在我们面前。这是.NET开发人员的幸运! 
    在我们开发asp.net应用程序时, System.Web.UI.Page是我们最熟悉并用的最多的一个类。但有多少人真正对这个类的源代码仔细研究过? 从相关搜索中可以看出并不是很多,比如,用Google搜索ProcessRequestMain方法中开始的“OnPageStartSessionObjects”,结果只有四个。
     今天我花了半天时间,研究了Page中处理请求的最关键的方法:ProcessRequestMain(),在这里我将自己的理解写出来与大家共享,欢迎大家批评并指正。一切尽在代码注释中:

注:为了方便阅读与理解,已去掉源代码中输出Trace信息的部分。

None.gifpublic class Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
private void ProcessRequestMain()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.IsInAspCompatMode)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                AspCompatApplicationStep.OnPageStartSessionObjects();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//将当前Context的Session中的对象传递给asp的OnStartPage
InBlock.gif            
//通过<%@ ASPCOMPAT="true" %>进行设置
InBlock.gif            
//参考文章: http://samples.gotdotnet.com/quickstart/aspplus/doc/cominterop.aspx
InBlock.gif

InBlock.gif            
this._requestValueCollection = this.DeterminePostBackMode();
InBlock.gif            
//检查PostBackMode, 如果启用了PostBack, 获取VIEWSTATE数据并赋值给_requestValueCollection
InBlock.gif

InBlock.gif            
base.InitRecursive(null);
InBlock.gif            
//调用基类的InitRecursive方法通过递归对子控件进行初始化, 比如: 生成控件ID,设置控件的Page属性。
InBlock.gif            
//OnInit()方法将会在此时被调用
InBlock.gif
            
InBlock.gif            
if (this.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.LoadPageViewState();
InBlock.gif                
//从_requestValueCollection通过反序列化载入视图状态数据,如果页面的Layout发生了改变,子控件重新递归载入(LoadViewStateRecursive)视图状态
InBlock.gif                
//载入后,从视图状态数据从得到所有要处理PostBack的控件并注册到_controlsRequiringPostBack. 
InBlock.gif

InBlock.gif                
this.ProcessPostData(this._requestValueCollection, true);
InBlock.gif                
//处理PostBack数据, 从PostBack数据中得到所有控件ID并检查每个控件,如果不能在当前页面中找到该控件(FindControl), 将其存入_leftoverPostData.
InBlock.gif                
//如果存在该控件,继续检查,若该控件没有实现IPostBackDataHandler, 但实现了 IPostBackEventHandler,注册该控件进行事件处理。
InBlock.gif                
//若该控件实现了System.Web.UI.IPostBackDataHandler,该控件的LoadPostData()方法在此时被调用,并将其加入到_changedPostDataConsumers,
InBlock.gif                
//并从._controlsRequiringPostBack(LoadPageViewState时对它进行了赋值)中移除该控件.
InBlock.gif                
//这样就从_controlsRequiringPostBack中移除了所有实现IPostBackDataHandler接口的控件.
InBlock.gif                
//接着继续对 _controlsRequiringPostBack中余下的控件进行处理,但奇怪的是又对这些余下的控件检查是否存在并实现了IPostBackDataHandler, 如果实现,
InBlock.gif                
//调用该控件的LoadPostData()(有点多此一举了, 可能是Relector生成的代码有误)并放入_changedPostDataConsumers,如果没实现,放入一个新ArrayList变量,
                    //检查结束后,将其赋值给
_controlsRequiringPostBack.那现在_controlsRequiringPostBack中剩下什么呢?没有实现IPostBackDataHandler, 
                    //但实现了 IPostBackEventHandler的控件以及
没有被Load的控件, 也就是在PostBack数据中存在但FindControl没有找到的控件。
                    //为什么会有找不到的控件呢?我们这里需要注意的是OnLoad()事件还没执行,
InBlock.gif                
//有些控件还没有被加载.下面的base.LoadRecursive()就是触发OnLoad()事件的。
InBlock.gif

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
base.LoadRecursive();
InBlock.gif            
//触发页面的OnLoad()事件->递归触发子控件的OnLoad()事件->将页面的_controlState状态设置为ControlState.Loaded
InBlock.gif

InBlock.gif            
if (this.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ProcessPostData(this._leftoverPostData, false);
InBlock.gif                
//理解了ProcessPostData(this._requestValueCollection, true)之后, 这个就很好理解了,就是检查_controlsRequiringPostBack中的控件是否存在并实现了
InBlock.gif                
//IPostBackDataHandler, 如果实现,调用该控件的LoadPostData()方法并将其加入到_changedPostDataConsumers.ProcessPostData(this._leftoverPostData, false)
InBlock.gif                
//这个方法就是为OnLoad()之后加载的控件服务的。
InBlock.gif
                     }
InBlock.gif                
this.RaiseChangedEvents();
InBlock.gif                
//在_changedPostDataConsumers(在两个ProcessPostData方法中向它添加了数据)中没有实现IPostBackDataHandler接口的控件触发RaisePostDataChangedEvent.
InBlock.gif

InBlock.gif                
this.RaisePostBackEvent(this._requestValueCollection);
InBlock.gif                
//触发_registeredControlThatRequireRaiseEvent及PostBack数据中实现IPostBackEventHandler接口的控件的RaisePostBackEvent事件。
InBlock.gif
        
        
InBlock.gif            
base.PreRenderRecursiveInternal();
InBlock.gif            
//首先调用EnsureChildControls,检查子控件是否创建, 如果没有, 调用进行创建CreateChildControls.
InBlock.gif            
//触发OnPreRende事件.
InBlock.gif            
//递归调用子控件的PreRenderRecursiveInternal方法
InBlock.gif            
//设置._controlState为 ControlState.PreRendered
InBlock.gif

InBlock.gif            
this.SavePageViewState();
InBlock.gif            
//保存视图状态数据至._viewStateToPersist 
InBlock.gif

InBlock.gif            
base.RenderControl(this.CreateHtmlTextWriter(this.Response.Output));
InBlock.gif            
//输出当前及所有子控件的内容
InBlock.gif

ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (ThreadAbortException)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.UnloadRecursive(true);
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (ConfigurationException)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception exception1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_DURING_REQUEST);
InBlock.gif            PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_TOTAL);
InBlock.gif            
if (!this.HandleError(exception1))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/dudu/archive/2005/10/21/259328.html

[Win32Exception (0x80004005): 系统找不到指定的文件。] System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) +950 System.Diagnostics.Process.Start(ProcessStartInfo startInfo) +64 OA.aspx.DsoFramer.ReadFile.ConvertToHtml(String docPath) in D:\YJC\仿通达OA源码\OA源代码修改版\FTD.Web.UI\aspx\DsoFramer\ReadFile.aspx.cs:42 ASP.aspx_dsoframer_readfile_aspx.__Renderform1(HtmlTextWriter __w, Control parameterContainer) in d:\YJC\仿通达OA源码\OA源代码修改版\FTD.Web.UI\aspx\DsoFramer\ReadFile.aspx:113 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +117 System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +208 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +47 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +132 System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +69 ASP.aspx_dsoframer_readfile_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\YJC\仿通达OA源码\OA源代码修改版\FTD.Web.UI\aspx\DsoFramer\ReadFile.aspx:22 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +117 System.Web.UI.Page.Render(HtmlTextWriter writer) +39 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +132 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +9480
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值