我們知道,ASP.NET 頁面請求的處理過程需要使用一些臨時檔案,這些檔案對我們分析頁面程式邏輯,有很大的協助。下邊我就說兩種方法:
第一種,在頁面的Page_Load方法中加入
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(this.GetType().Assembly.Location);
}
會輸出類似於下面的字串:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\website13\63ed8704\93b8f11b\App_Web_rixp4_rs.dll
我們去掉最後的App_Web_rixp4_rs.dll字元,將剩下的字串複製到“運行”視窗,開啟相應的檔案夾。
我們以default.aspx頁面為例,在開啟的檔案夾中找到類似於這樣的檔案default2.aspx.cdcab7d2.compiled。
我們開啟這個檔案,會看到類似於下面的內容:
<?xml version="1.0" encoding="utf-8"?>
<preserve resultType="3" virtualPath="/WebSite13/Default.aspx" hash="73ae13447" filehash="ffffe207cb3bfc04" flags="110000" assembly="App_Web_rixp4_rs" type="ASP.default_aspx">
<filedeps>
<filedep name="/WebSite13/Default.aspx" />
</filedeps>
</preserve>
在開啟的檔案中,我們找到assembly="App_Web_rixp4_rs",在剛才開啟的檔案中找以App_Web_rixp4_rs開頭的類檔案。這些檔案就是我們需要的臨時檔案。
第二種,我們寫一個繼承IHttpHandlerFactory介面的檔案
public class HttpHandlerFactory : IHttpHandlerFactory
{
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
//得到編譯執行個體(通過反射)
PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory), true);
IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated);
ResponseAssemblyLocation(context, handler);
//返回
return handler;
}
public virtual void ReleaseHandler(IHttpHandler handler)
{
}
[Conditional("DEBUG")]
private void ResponseAssemblyLocation(HttpContext context, IHttpHandler handler)
{
context.Response.Write(handler.GetType().Assembly.Location);
}
}
然後在web.config進行配置一下: <httpHandlers>
<add verb="*" path="*.aspx" validate="false" type="HttpHandlerFactory"/>
</httpHandlers>
<compilation debug="true"/>
在debug="true"的情況下,類HttpHandlerFactory中的ResponseAssemblyLocation方法就能執行,反之,不執行。
需要說明的一點是,我產生的類檔案HttpHandlerFactory在網站的App_Code目錄,故web.config檔案中的type節未配置Assembly名稱。