快速查找ASP.NET产生的临时文件

我们知道,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名称。
原文地址:https://www.cnblogs.com/fengfeng/p/1251192.html