您的位置:首页 > 编程语言 > ASP

asp.net 生成静态html页面

2012-06-21 09:23 716 查看
方案:

1,利用模板生成html 

2,将文件title和文件名称保存到数据库

生成.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Web.UI.HtmlControls;

public partial class CMS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Unnamed1_Click(object sender, EventArgs e)
{
WriteFile("hello world", "");
}
public void WriteFile(string strText,string strAuthor)
{
String path = Server.MapPath("news/");
Encoding code = Encoding.GetEncoding("gb2312");
//读取模板
String temp = Server.MapPath("Template.htm");
StreamReader sr = null;
StreamWriter sw = null;
String str = "";
try
{
sr = new StreamReader(temp, code);
//读取文件
str = sr.ReadToEnd();

}
catch (Exception exp)
{

HttpContext.Current.Response.Write(exp.Message);

HttpContext.Current.Response.End();

sr.Close();
}
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
//替换内容
str = str.Replace("$replacecontent", strText);
try
{
sw = new StreamWriter(path + filename, false, code);
sw.Write(str);
sw.Flush();
Response.Write("<a href='./news/" + filename + "'>strText</a>");

}
catch (Exception exp)
{

HttpContext.Current.Response.Write(exp.Message);

HttpContext.Current.Response.End();

sr.Close();
}
finally
{
sw.Close();
}

}
}


模板  Template.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>新闻中心</title>
</head>
<body>
$replacecontent
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息