静态页面生成方案
新闻表T_Artice
包含字段如下:
Id 新闻ID
Title 标题
Content 内容
Date 日期
Author 作者
LinkHtml 对应静态页面
//HasUpdate 是否有更新
添加新闻时,生成其静态页面,并将链接地址记录在数据库中
更新新闻时,将当前新闻指向的静态页面删除,并生成新的静态页面,然后更新链接地址并记录在库。
Theme.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>
<table>
<tr>
<td style="width: 100px">
用户名</td>
<td style="width: 100px">@ppt[0]@
</td>
</tr>
<tr>
<td style="width: 100px">
IP</td>
<td style="width: 100px">@ppt[1]@
</td>
</tr>
<tr>
<td style="width: 100px">
信息</td>
<td style="width: 100px">@ppt[2]@
</td>
</tr>
<tr>
<td style="width: 100px">
页面</td>
<td style="width: 100px">@ppt[3]@
</td>
</tr>
</table>
</body>
</html>
WriteHtml.aspx代码
Random rand = new Random();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Ads.BLL.T_Log bllLog = new Ads.BLL.T_Log();
DataSet ds = new DataSet();
ds = bllLog.GetAllList();
if (ds != null)
{
lblMsg.Text = "<table>";
foreach (DataRow row in ds.Tables[0].Rows)
{
string file = Write(row["UserName"].ToString(), row["UserIp"].ToString(), row["Page"].ToString(), row["Message"].ToString());
lblMsg.Text += "<tr><td><a href=" + file + " target=_blank>" + row["UserName"].ToString() + "</a>";
}
lblMsg.Text += "</table>";
}
}
}
private string Write(string userName,string ip,string page,string msg)
{
string filesName="";
string[] format = new string[4];//定义和htmlyem标记数目一致的数组
StringBuilder htmltext=new StringBuilder();
try
{
using (StreamReader sr = new StreamReader(Server.MapPath("Theme.htm")))
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}
}
catch
{
Response.Write("<Script>alert(~读取文件错误~)</Script>");
}
format[0]=userName;
format[1]= ip;
format[2]=msg;
format[3]= page;
for (int i = 0; i < 4; i++)
{
htmltext.Replace("@ppt[" + i + "]@", format[i]);
}
try
{
string s = rand.Next(999999).ToString();
string fileName = DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + s + ".htm";
filesName = fileName;
fileName = Server.MapPath(fileName);
using(StreamWriter sw=new StreamWriter(fileName,false,System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
}
catch
{
Response.Write ("The file could not be wirte:");
}
return filesName;
}