面试的时候,总谈到asp.net的声明周期。这涉及到一些原理性的问题。
新建一个webform工程.在default.aspx.cs 的load中写如下的话:说明如下
//1.获取模块的完整路径。 C:\\Program Files (x86)\\IIS Express\\iisexpress.exe
可见新来一个请求的时候,都是启动的这个exe,这个exe注入了我们代码编译好的dll
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
//2.获取和设置当前目录(该进程从中启动的目录)的完全限定目录 C:\\Program Files (x86)\\IIS Express
string path2 = System.Environment.CurrentDirectory;
//3.获取应用程序的当前工作目录 C:\\Program Files (x86)\\IIS Express
string path3 = System.IO.Directory.GetCurrentDirectory();
//4.获取程序的基目录 编译出的dll文件夹
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
//5.获取和设置包括该应用程序的目录的名称 编译出的dll文件夹
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
上面是后台。当我们请求aspx文件的时候发生了什么呢?
aspx 是这个handler 处理的(系统下的web.config):
<httpHandlers>
<add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="True" />
看看这个Factory 里面最后返回一个Ihtphandler
private IHttpHandler GetHandlerHelper(HttpContext context, string requestType, VirtualPath virtualPath, string physicalPath)
{
Page page = BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page), context, true) as Page;
if (page == null)
{
return null;
}
page.TemplateControlVirtualPath = virtualPath;
return page;
}
一层一层看去--->,最终会到这里:
// System.Web.Compilation.BuildManager
using System.CodeDom.Compiler;
using System.Web.Util;
private BuildResult CompileWebFile(VirtualPath virtualPath)
{
BuildResult buildResult = null;
string text = null;
if (this._topLevelFilesCompiledCompleted)
{
VirtualPath parent = virtualPath.Parent;
if (this.IsBatchEnabledForDirectory(parent))
{
this.BatchCompileWebDirectory(null, parent, true);
text = BuildManager.GetCacheKeyFromVirtualPath(virtualPath);
buildResult = this._memoryCache.GetBuildResult(text);
if (buildResult == null && DelayLoadType.Enabled)
{
buildResult = BuildManager.GetBuildResultFromCache(text);
}
if (buildResult != null)
{
if (buildResult is BuildResultCompileError)
{
throw ((BuildResultCompileError)buildResult).CompileException;
}
return buildResult;
}
}
}
DateTime utcNow = DateTime.UtcNow;
string outputAssemblyName = "App_Web_" + BuildManager.GenerateRandomAssemblyName(BuildManager.GetGeneratedAssemblyBaseName(virtualPath), false);
BuildProvidersCompiler buildProvidersCompiler = new BuildProvidersCompiler(virtualPath, outputAssemblyName);
就是App_web_XXXXXXX.
我们在前台这样写:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebAspx._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h5> <%=System.Reflection.Assembly.GetExecutingAssembly().Location %> </h5>
<h4> <%=DateTime.Now.ToString("yyyy-MM-dd") %> </h4>
最终出现h5 标签内是
C:\Users\Administrator\AppData\Local\Temp\Temporary ASP.NET Files\vs\165362fb\27bd23a8\App_Web_default.aspx.cdcab7d2.miqmp5pm.dll
反编译可以看到:
public class default_aspx : _Default, IRequiresSessionState, IHttpHandler
而_Default是aspx.cs
namespace WebAspx
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
就是前台是后台的子类,并且不再同一个dll中!所以啊,前台里面能写<%=后台代码,或者后台public 变量%>
default_aspx这里面有渲染等东西,也有我们上面写的那个h5 标签。下面还有这个函数
private void __RenderBodyContent(HtmlTextWriter __w, Control parameterContainer)
{
this.__PageInspector_BeginRenderTracing(new object[5]
{
__w,
"/Default.aspx",
242,
7,
true
});
__w.Write("\r\n<h5> ");
this.__PageInspector_EndRenderTracing(new object[1]
{
__w
});
this.__PageInspector_BeginRenderTracing(new object[5]
{
__w,
"/Default.aspx",
249,
64,
false
});
__w.Write(Assembly.GetExecutingAssembly().Location);
this.__PageInspector_EndRenderTracing(new object[1]
{
__w
});
this.__PageInspector_BeginRenderTracing(new object[5]
{
__w,
"/Default.aspx",
313,
17,
true
});
__w.Write(" </h5>\r\n <h4> ");
this.__PageInspector_EndRenderTracing(new object[1]
{
__w
});
this.__PageInspector_BeginRenderTracing(new object[5]
{
__w,
"/Default.aspx",
330,
41,
false
});
__w.Write(DateTime.Now.ToString("yyyy-MM-dd"));
this.__PageInspector_EndRenderTracing(new object[1]
{
__w
});
__PageInspector_BeginRenderTracing这个东西,是什么意思呢?
其实可以猜出来,就是后台代码的执行结果最终渲染到前台所在的位置和长度,这个default.aspx文件比较长,但在这个函数中,后台代码的部分有显现,那么多html呢?就靠这个东西去读了吧?
欢迎指正!