靜態頁面產生方案
新聞表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;
}