在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
上一篇说到applicationInstance会执行一些列的事件。下面是我在msdn上找到有关asp.net程序生命周期相关的描述及图片 声明周期的起始 ASP.NET 处理已映射到其上的文件扩展名,如 .aspx、.ascx、.ashx 和 .asmx。 ASP.NET 接收对应用程序的第一个请求 HostingEnvironment的类创建一个实例,该实例提供对有关应用程序的信息(如存储该应用程序的文件夹的名称)的访问。
为每个请求创建 ASP.NET 核心对象 HttpResponse 对象包含发送到客户端的响应,包括所有呈现的输出和 Cookie。 HttpApplication 对象分配给请求 HttpApplication 类派生)的一个实例,并使用该派生类表示应用程序。 注意
HttpApplication 实例。
Init 方法。
由 HttpApplication 管线处理请求。 HttpApplication 类的开发人员尤其需要注意这些事件。
好了,不贴人家的东西了,感兴趣的可以到原网站去看一下。我们重点就是看一下HttpApplication管线处理请求。下面是对事件的解释
看一下上面的管线处理请求中标号的10和15,分别是创建前台页面类和调用页面类的ProcessRequest方法的地方。这在我们的上篇文章中说到了。下面我们重点说Application_Start方法执行的地方,ProcessRequest方法都做了什么。 我们都知道Application_Start是程序的入口地方,类似与我们的main函数。但是我在msdn上也没能查到Application_Start是在哪里调用了,但是我们似乎是可以找到这个方法调用的地方,让我们来反编译HttpRuntime(为什么选择反编译这个类?上篇文章里面会有答案)的ProcessRequestNow()方法,继续看_theRuntime.ProcessRequestInternal(wr);我们看ProcessRequestInternal方法中的IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(context);,我们可以看出来HttpApplicationFactory是个静态类,我们可以看到该静态类里面有成员internal const string applicationFileName = "global.asax";private static HttpApplicationFactory _theApplicationFactory;private bool _appOnStartCalled;这里我们知道了为什么global.asax文件为什么不能修改文件名,因为这里已经写死了。而后面_appOnStartCalled变量记录着程序的application_start方法是否被调用过,如没被调用过,则为false,进而调用程序的application_start,反之则不进行调用。看GetApplicationInstance中会有判断过程_theApplicationFactory.EnsureAppStartCalled(context);。所以程序的Application_Start()方法的执行顺序肯定先于所有的管线处理请求中的事件。 下面我们来看一下页面的ProcessRequest方法都做了什么操作。也就是开始页面生命周期。 我新建了一个ReflectWeb项目,又添加了demo.aspx页面,页面内容如下 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="ReflectorWeb.Demo" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8 <title></title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <div> 13 程序集位置:<%=this.GetType().Assembly.Location %><br /> 14 TextBox1:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> 15 TextBox2:<input type="text" runat="server" id="TextBox2" /><br /> 16 TextBox3:<input type="text" id="TextBox3" /><br /> 17 </div> 18 </form> 19 </body> 20 </html> 后台类内容很简单 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 namespace ReflectorWeb 9 { 10 public partial class Demo : System.Web.UI.Page 11 { 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 Console.WriteLine("test console"); 15 //byte[] tempData = System.Text.Encoding.UTF8.GetBytes("测试Page_Load方法中使用Response.Write方法输出"); 16 Response.Write("测试Page_Load方法中使用Response.Write方法输出"); 17 } 18 } 19 } 而页面效果如下 下面我们来反编译一下这个程序集,看一下这个程序集里面的内容 程序集里面有个demo_aspx类,这就是我们前台页面生成后的类了,页面类代码如下: 1 [CompilerGlobalScope] 2 public class demo_aspx : Demo, IRequiresSessionState, IHttpHandler 3 { 4 // Fields 5 private static object __fileDependencies; 6 private static bool __initialized; 7 private static MethodInfo __PageInspector_BeginRenderTracingMethod; 8 private static MethodInfo __PageInspector_EndRenderTracingMethod; 9 private static MethodInfo __PageInspector_SetTraceDataMethod; 10 11 // Methods 12 static demo_aspx(); 13 [DebuggerNonUserCode] 14 public demo_aspx(); 15 [DebuggerNonUserCode] 16 private LiteralControl __BuildControl__control2(); 17 [DebuggerNonUserCode] 18 private HtmlHead __BuildControl__control3(); 19 [DebuggerNonUserCode] 20 private HtmlMeta __BuildControl__control4(); 21 [DebuggerNonUserCode] 22 private HtmlTitle __BuildControl__control5(); 23 [DebuggerNonUserCode] 24 private LiteralControl __BuildControl__control6(); 25 [DebuggerNonUserCode] 26 private LiteralControl __BuildControl__control7(); 27 [DebuggerNonUserCode] 28 private HtmlForm __BuildControlform1(); 29 [DebuggerNonUserCode] 30 private TextBox __BuildControlTextBox1(); 31 [DebuggerNonUserCode] 32 private HtmlInputText __BuildControlTextBox2(); 33 [DebuggerNonUserCode] 34 private void __BuildControlTree(demo_aspx __ctrl); 35 private void __PageInspector_BeginRenderTracing(object[] parameters); 36 private void __PageInspector_EndRenderTracing(object[] parameters); 37 private static MethodInfo __PageInspector_LoadHelper(string helperName); 38 private void __PageInspector_SetTraceData(object[] parameters); 39 private void __Renderform1(HtmlTextWriter __w, Control parameterContainer); 40 [DebuggerNonUserCode] 41 protected override void FrameworkInitialize(); 42 [DebuggerNonUserCode] 43 public override int GetTypeHashCode(); 44 [DebuggerNonUserCode] 45 public override void ProcessRequest(HttpContext context); 46 47 // Properties 48 protected HttpApplication ApplicationInstance { get; } 49 protected DefaultProfile Profile { get; } 50 } 51 52 53 Expand Methods 54 注意一下,该类继承自Demo类,也就是后台类,而后台类又继承自Page类。下面进入ProcessRequest方法 又调用了父类的ProcessRequest方法base.ProcessRequest(context);这个Page类的一个方法,看这个类的组成 1 [Designer("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, Microsoft.VisualStudio.Web, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(IRootDesigner)), DefaultEvent("Load"), ToolboxItem(false), DesignerCategory("ASPXCodeBehind"), DesignerSerializer("Microsoft.VisualStudio.Web.WebForms.WebFormCodeDomSerializer, Microsoft.VisualStudio.Web, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.TypeCodeDomSerializer, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] 2 public class Page : TemplateControl, IHttpHandler 3 { 4 // Fields 5 internal HttpApplicationState _application; 6 private bool _aspCompatMode; 7 private AspCompatApplicationStep _aspCompatStep; 8 private bool _asyncMode; 9 private PageAsyncTaskManager _asyncTaskManager; 10 private TimeSpan _asyncTimeout; 11 private bool _asyncTimeoutSet; 12 private Control _autoPostBackControl; 13 internal Cache _cache; 14 private bool _cachedRequestViewState; 15 private ICallbackEventHandler _callbackControl; 16 private ArrayList _changedPostDataConsumers; 17 private string _clientQueryString; 18 private ClientScriptManager _clientScriptManager; 19 private string _clientState; 20 private bool _clientSupportsJavaScript; 21 private bool _clientSupportsJavaScriptChecked; 22 private string _clientTarget; 23 private bool _containsCrossPagePost; 24 private bool _containsEncryptedViewState; 25 private IDictionary _contentTemplateCollection; 26 internal HttpContext _context; 27 private ArrayList _controlsRequiringPostBack; 28 private StringSet _controlStateLoadedControlIds; 29 private Stack _dataBindingContext; 30 private string _descriptionToBeSet; 31 private bool _designMode; 32 private bool _designModeChecked; 33 private CultureInfo _dynamicCulture; 34 private CultureInfo _dynamicUICulture; 35 private ArrayList _enabledControls; 36 private bool _enableEventValidation; 37 private bool _enableViewStateMac; 38 private ViewStateEncryptionMode _encryptionMode; 39 internal string _errorPage; 40 private Control _focusedControl; 41 private string _focusedControlID; 42 private bool _fOnFormRenderCalled; 43 private HtmlForm _form; 44 private bool _fPageLayoutChanged; 45 private bool _fPostBackScriptRendered; 46 private bool _fRequirePostBackScript; 47 private bool _fRequireWebFormsScript; 48 private bool _fWebFormsScriptRendered; 49 private bool _haveIdSeparator; 50 private HtmlHead _header; 51 internal Dictionary<string, string> _hiddenFieldsToRender; 52 private char _idSeparator; 53 private bool _inOnFormRender; 54 private bool _isCallback; 55 private bool _isCrossPagePostBack; 56 private IDictionary _items; 57 private string _keywordsToBeSet; 58 private NameValueCollection _leftoverPostData; 59 private LegacyPageAsyncInfo _legacyAsyncInfo; 60 private LegacyPageAsyncTaskManager _legacyAsyncTaskManager; 61 private bool _maintainScrollPosition; 62 private MasterPage _master; 63 private VirtualPath _masterPageFile; 64 private static readonly TimeSpan _maxAsyncTimeout; 65 private int _maxPageStateFieldLength; 66 private ModelBindingExecutionContext _modelBindingExecutionContext; 67 private ModelStateDictionary _modelState; 68 private bool _needToPersistViewState; 69 private PageAdapter _pageAdapter; 70 private SimpleBitVector32 _pageFlags; 71 private Stack _partialCachingControlStack; 72 private PageStatePersister _persister; 73 private RenderMethod _postFormRenderDelegate; 74 private bool _preInitWorkComplete; 75 private Page _previousPage; 76 private VirtualPath _previousPagePath; 77 private bool _profileTreeBuilt; 78 internal HybridDictionary _registeredControlsRequiringClearChildControlState; 79 internal ControlSet _registeredControlsRequiringControlState; 80 private ArrayList _registeredControlsThatRequirePostBack; 81 private IPostBackEventHandler _registeredControlThatRequireRaiseEvent; 82 private string _relativeFilePath; 83 internal HttpRequest _request; 84 private NameValueCollection _requestValueCollection; 85 private string _requestViewState; 86 private bool _requireFocusScript; 87 private bool _requireScrollScript; 88 internal HttpResponse _response; 89 private static Type _scriptManagerType; 90 private int _scrollPositionX; 91 private const string _scrollPositionXID = "__SCROLLPOSITIONX"; 92 private int _scrollPositionY; 93 private const string _scrollPositionYID = "__SCROLLPOSITIONY"; 94 private HttpSessionState _session; 95 private bool _sessionRetrieved; 96 private SmartNavigationSupport _smartNavSupport; 97 private PageTheme _styleSheet; 98 private string _styleSheetName; 99 private int _supportsStyleSheets; 100 private PageTheme _theme; 101 private string _themeName; 102 private string _titleToBeSet; 103 private int _transactionMode; 104 private string _uniqueFilePathSuffix; 105 private UnobtrusiveValidationMode? _unobtrusiveValidationMode; 106 private NameValueCollection _unvalidatedRequestValueCollection; 107 private bool _validated; 108 private string _validatorInvalidControl; 109 private ValidatorCollection _validators; 110 private bool _viewStateEncryptionRequested; 111 private string _viewStateUserKey; 112 private XhtmlConformanceMode _xhtmlConformanceMode; 113 private bool _xhtmlConformanceModeSet; 114 internal const bool BufferDefault = true; 115 internal const string callbackID = "__CALLBACKID"; 116 internal const string callbackIndexID = "__CALLBACKINDEX"; 117 internal const string callbackLoadScriptID = "__CALLBACKLOADSCRIPT"; 118 internal const string callbackParameterID = "__CALLBACKPARAM"; 119 internal static readonly int DefaultAsyncTimeoutSeconds; 120 internal static readonly int DefaultMaxPageStateFieldLength; 121 private const string EnabledControlArray = "__enabledControlArray"; 122 internal const bool EnableEventValidationDefault = true; 123 internal const bool EnableViewStateMacDefault = true; 124 internal const ViewStateEncryptionMode EncryptionModeDefault = ViewStateEncryptionMode.Auto; 125 internal static readonly object EventInitComplete; 126 internal static readonly object EventLoadComplete; 127 internal static readonly 全部评论
专题导读
上一篇:[ASP.NETMVC3.0]ContactManager之迭代开发二发布时间:2022-07-10下一篇:ASP.NETWebAPI上实现WebSocketASP.NETWebAPI上实现WebSocket发布时间:2022-07-10热门推荐
热门话题
阅读排行榜
|
请发表评论