最近看了很多关于自动生成HTML静态页面的介绍,大多数都使用模板的方法,但模板方式只能应用于一些简单的页面,对于复杂的页面就显示无能为力了,例如页面中有DataList,GridView等控件时,用模板显然解决不了问题,利用页面输出很方便。
public void createHTML()
{
string filePath = Server.MapPath(".") + "index.html";
//调用default.aspx, 并获取其输出
StringWriter sw = new StringWriter();
Server.Execute("Default.aspx", sw);
string content = sw.ToString();
//输出到客户端
Response.Write(content);
Response.Flush();
//写进文件
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write))
{
using (StreamWriter swrite = new StreamWriter(fs, Response.ContentEncoding))
{
swrite.Write(content);
}
}
}
finally
{
}
}
简单方便
利用页面输出生成HTML静态页面
最新推荐文章于 2021-06-04 01:35:51 发布
本文介绍了一种简单的方法来生成包含复杂元素如DataList和GridView的HTML静态页面。通过调用特定页面并捕获其输出内容,可以有效地创建静态副本,这种方法适用于需要缓存动态生成页面的场景。
1万+

被折叠的 条评论
为什么被折叠?



