在windows应用程序中运行asp.net程序的下级目录问题

重载SimpleWorkerRequest


为了能运行下级目录的aspx文件,我们不能象解决中文乱码的方式重载,需要重写重多的方法。当然我也不清楚需要重写哪些方法,我想当然的重写了几个方法后,依然不然,最后只好让Cassini来告诉我了。当然让她告诉我,我只好耐心的在几乎每个方法上添加Trace.WriteLine方法。我得到了以下"提示":



[3]Process...
[4]MapPath(String path="/mag/Maintain/AdList.aspx") should return D:\Magazine\Mag.Web.App\Maintain\AdList.aspx...
[5]GetAppPath() should return /mag
[6]MapPath(String path="/mag") should return D:\Magazine\Mag.Web.App...
[7]MapPath(String path="") should return C:\WINDOWS\system32...
[8]GetAppPath() should return /mag
[9]GetAppPath() should return /mag
[10]GetFilePath() should return /mag/Maintain/AdList.aspx
[11]MapPath(String path="/mag/Maintain/AdList.aspx") should return D:\Magazine\Mag.Web.App\Maintain\AdList.aspx...
[12]MapPath(String path="/mag/Maintain") should return D:\Magazine\Mag.Web.App\Maintain...
[13]GetAppPath() should return /mag
[14]MapPath(String path="/mag/Maintain/AdList.aspx") should return D:\Magazine\Mag.Web.App\Maintain\AdList.aspx...
[15]GetAppPathTranslated() should return D:\Magazine\Mag.Web.App\
"141cf04": 已加载"c:\windows\assembly\gac\system.web.regularexpressions\1.0.5000.0__b03f5f7f11d50a3a\system.web.regularexpressions.dll",未加载符号。
[16]GetAppPath() should return /mag
....
[20]MapPath(String path="/mag/global.asax") should return D:\Magazine\Mag.Web.App\global.asax...
...
[24]MapPath(String path="") should return C:\WINDOWS\system32...
[25]GetAppPathTranslated() should return D:\Magazine\Mag.Web.App\
...
[29]GetUriPath() should return /mag/Maintain/AdList.aspx
[30]GetFilePathTranslated() should return D:\Magazine\Mag.Web.App\Maintain\AdList.aspx
[31]MapPath(String path="/mag/Maintain") should return D:\Magazine\Mag.Web.App\Maintain...
[32]MapPath(String path="/mag/Maintain/CustomServiceList.aspx") should return D:\Magazine\Mag.Web.App\Maintain\CustomServiceList.aspx...
...
"141cf04": 已加载"c:\windows\assembly\gac\mscorlib.resources\1.0.5000.0_zh-chs_b77a5c561934e089\mscorlib.resources.dll",未加载符号。
"141cf04": 已加载"c:\windows\assembly\gac\system.data.resources\1.0.5000.0_zh-chs_b77a5c561934e089\system.data.resources.dll",未加载符号。
[65]SendResponseFromMemory...
[66]SendResponseFromMemory...
[67]SendResponseFromMemory...
[68]FlushResponse...
[69]SendResponseFromMemory...
[70]FlushResponse...


省略了一些输出,大家可以留意一下以"[数字]"开头的行。于是我就有了足够的信息去重写了,为了方便测试,我因应我的环境,固定了两个变量

/**/ ///  
/// 测试用。asp.net应用程序所在的目录 
/// 
 

public   const   string  APP_FOLD  =   @" D:\Magazine\Mag.Web.App "
/**/ ///  
/// 测试用。asp.net的虚拟应用程序名 
/// 
 

public   const   string  APP_VIRT  =   " /foo " ;

以下是完整的代码

     public   class  MyRequest:SimpleWorkerRequest 
    
{
        
/**//// 
        
/// 测试用。asp.net应用程序所在的目录
        
/// 

        public const string APP_FOLD = @"D:\Magazine\Mag.Web.App";
        
/**//// 
        
/// 测试用。asp.net的虚拟应用程序名
        
/// 

        public const string APP_VIRT = "/foo";

        
private string _filePath;
        
private string _path;
        
        
private ArrayList        _responseBodyBytes;
        
private FileStream        _file;
        
private System.IO.MemoryStream mem;
        
private System.IO.StreamWriter writer;
        
public MyRequest(string pWebPage, string pQuery,string pFile):base(String.Empty,pQuery,null)
        
{
            
this.mem = new MemoryStream();

//            this.writer = new StreamWriter(pFile,false,Encoding.GetEncoding("gb2312"));
            this.writer = new StreamWriter(pFile,false,Encoding.UTF8);
            
this._responseBodyBytes = new ArrayList();
            
this.initFilePath(pWebPage);
        }


        
private void initFilePath(string path)
        
{
            
this._path = path;            
            
string str = string.Format("{0}\\{1}",APP_FOLD, path);            
            str 
= str.Replace("/","\\");
            
this._filePath = str;    
        }


        
重写HttpWorkerRequest相关方法#region 重写HttpWorkerRequest相关方法

        
public override String GetUriPath() 
        
{
            
return APP_VIRT + "/" + _path;
        }
        

        
public override String GetFilePath() 
        
{
            
return APP_VIRT + "/" +  _path;
        }


        
public override String GetFilePathTranslated() 
        
{
            
return this._filePath;
        }


        
public override String GetAppPath() 
        
{
            
return APP_VIRT;
        }


        
public override String GetAppPathTranslated() 
        
{
            
return  APP_FOLD;
        }



    
        
public override void SendResponseFromMemory(byte[] data, int length) 
        
{
            
if (length > 0
            
{
                
byte[] bytes = new byte[length];
                Buffer.BlockCopy(data, 
0, bytes, 0, length);
                _responseBodyBytes.Add(bytes);
            }

        }


        
public override void FlushResponse(bool finalFlush) 
        
{
            
for (int i = 0; i < _responseBodyBytes.Count; i++
            
{
                
byte[] bytes = (byte[])_responseBodyBytes[i];                

                
this.mem.Write(bytes,0,bytes.Length);            
            }



            _responseBodyBytes 
= new ArrayList();

            
if (finalFlush) 
            
{
                System.IO.StreamReader read 
= new StreamReader(this.mem);
                
this.mem.Position = 0;
                
string str = read.ReadToEnd();
                
this.writer.Write(str);
                
this.writer.Close();        

            }

        }

        
#endregion

        
    }

 

 

转载于:https://www.cnblogs.com/kevin-Y/archive/2006/06/07/419609.html

你可能感兴趣的:(在windows应用程序中运行asp.net程序的下级目录问题)