内嵌同域的页面

本文探讨了在ASP.NET中实现页面嵌套的多种方法及其局限性,最终提出了一种有效的解决方案,通过自定义SimpleWorkerRequest类来正确处理XSL+XML生成的内容,并解决了字符编码导致的乱码问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方案1
<!--#include file="$path"-->
限制:其中$path是无法动态设置的,所以被否决

方案2
通过WebRequest获取页面内容,然后输出到页面上.
 限制:由于WebRequest在构建时是不支持相对路径的,必须要http://xxx形式的绝对路径,而我们对即将部署的环境一无所知,所以该方案也行不通

方案3
动态执行页面,把其内容写到最终目标上.
1. 增加自定义控件out.ascx, 防治到在需要内嵌的地方,同时把动态页面的参数通过属性设置到out类对象中.
2. 重写out的RenderControl方法
如果把对out的属性设置放在.aspx中,则out的属性将在Render阶段才生效.为了保险起见和方便用户,我们把页面渲染放到Render阶段.
3. 运行内嵌页面,渲染到目标页面.
由于需要内嵌的页面根据不同的out属性依赖不同的QueryString.所以之前的HttpContext不能重用了.
失败的子方案1,
PageParser.GetCompiledPageInstance产生IHttpHandler,然后执行IHttpHandler的ProcessRequest方法.
{
string url = Request.Url.AbsoluteUri;
url 
= url.Substring(0, url.LastIndexOf('/')+1);
string physicalPath = Request.PhysicalApplicationPath + "tabContent.ashx";
HttpRequest tabContentRes 
= new HttpRequest(physicalPath, url + "tabContent.ashx", String.Format(ashxQsFmt, cn, ticker));
HttpContext context 
= new HttpContext(tabContentRes , Response);
            IHttpHandler handle 
= PageParser.GetCompiledPageInstance(Request.ApplicationPath + "/tabContent.ashx", physicalPath, context);
            handle.ProcessRequest(context);

//结果输出全部变成编码后(HtmlEncode)的HTML代码
}

目前方案则简单的多
HttpRuntime.ProcessRequest();
  {
SimpleWorkerRequest contentRes 
= new SimpleWorkerRequest("tabContent.ashx", String.Format(ashxQsFmt, cn, ticker), writer);
 HttpRuntime.ProcessRequest(contentRes);      
}

该方法仍然有BUG,我的页面是XSL+XML生成目标代码的.结果调用 HttpRuntime.ProcessRequest产生的结果中,来自XSL文件中的 ISO Latin-1字符,如&#160; &#8482;转换出现乱码,成为"聼"和" 鈩?" .跟踪观察,其Request,Response.Output和CurrentThread.Encoding等均正常.

导致乱码的问题一般是encoding不符合。
经反复尝试发现,生成的代码是utf-8(网页一般都是这个),而simpleWorkRequst在进行输出时,编码采用的是Encoding.Default(在我机器上是-        Encoding.Default    {System.Text.DBCSCodePageEncoding}    System.Text.Encoding {System.Text.DBCSCodePageEncoding}),因此出现乱码。
解决方法,继承 SimpleWorkerRequest,重写了其SendResponseFromMemory方法
public   class  TestWorkerRequest : SimpleWorkerRequest
{
    
private TextWriter Output;
    
public TestWorkerRequest(string a1, string a2, TextWriter a3):base(a1, a2, a3)
    
{
        Output 
= a3;
    }

    
public override void SendResponseFromMemory(byte[] data, int length)
    
{
        Output.Write(Encoding.UTF8.GetChars(data, 
0, length));
        
    }

}

之前比较笨拙的做法(惭愧),也放上来吧
public   class  TestWorkerRequest : SimpleWorkerRequest
{
    
public TestWorkerRequest(string a1, string a2, TextWriter a3):base(a1, a2, a3)
    
{
    }

    
public MemoryStream OutPutStream = new MemoryStream();
    
public override void SendResponseFromMemory(byte[] data, int length)
    
{
       
OutPutStream .Write(data, 0, length);        
    }

}
使用时:
         byte [] byteArray  =  swr.OutputStream.GetBuffer();
        
int  count  =  byteArray.Length;
        
if  (count  >   0 )
        
{
            
char[] charArray = new char[count];
            Encoding.UTF8.GetDecoder().GetChars(
                    byteArray, 
0, count, charArray, 0);

            Response.Write(charArray, 
0, count);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值