方案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字符,如  ™转换出现乱码,成为"聼"和" 鈩?" .跟踪观察,其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);
}
<!--#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方法.











目前方案则简单的多
HttpRuntime.ProcessRequest();




该方法仍然有BUG,我的页面是XSL+XML生成目标代码的.结果调用 HttpRuntime.ProcessRequest产生的结果中,来自XSL文件中的 ISO Latin-1字符,如  ™转换出现乱码,成为"聼"和" 鈩?" .跟踪观察,其Request,Response.Output和CurrentThread.Encoding等均正常.
导致乱码的问题一般是encoding不符合。
经反复尝试发现,生成的代码是utf-8(网页一般都是这个),而simpleWorkRequst在进行输出时,编码采用的是Encoding.Default(在我机器上是- Encoding.Default {System.Text.DBCSCodePageEncoding} System.Text.Encoding {System.Text.DBCSCodePageEncoding}),因此出现乱码。
解决方法,继承 SimpleWorkerRequest,重写了其SendResponseFromMemory方法













之前比较笨拙的做法(惭愧),也放上来吧












使用时:









