大家有没有做过这样的项目?具体如何实现?
比如新闻部分生成静态后,如2005-05-16/103600214.htm后,如以后要用到此新闻,比如对它评论,怎么知道他对应的ID?
CreateHTMLFile chf = new CreateHTMLFile();
chf.read("../template/article_view.html");
chf.fill("{-page_title-}", art_title);
chf.fill("{-title-}", art_title);
chf.fill("{-author-}", author);
chf.fill("{-fromwhere-}", fromwhere);
chf.fill("{-datetime-}", DateTime.Now.ToString());
content = content.Replace("[pic]", "<img src=\"");
content = content.Replace("[/pic]", "\" />");
chf.fill("{-content-}", content);
string dirRealPath = Server.MapPath("../" + dirPath);
Directory.CreateDirectory(dirRealPath);
if (!chf.write("../" + dirPath + "/" + fileName))
msg("error.............");
msg("ok","article_admin.aspx?act=add");
//生成靜態網頁使用到的类
using System.IO;
using System.Text;
/// <summary>
/// CreateHTMLFile 的摘要说明
/// </summary>
public class CreateHTMLFile
{
string htmlCode;
Encoding code;
public CreateHTMLFile()
{
//
// TODO: 在此处添加构造函数逻辑
//
htmlCode = "";
code = Encoding.GetEncoding("gb2312");//HTML文件使用中文字符代码
}
public bool read(string templatePath)//读取模板的字符,并保存到htmlCode成员变量
{
try
{
templatePath = System.Web.HttpContext.Current.Server.MapPath(templatePath);//获取绝对路径
StreamReader sr = new StreamReader(templatePath, code);//创建StreamReader对象
htmlCode = sr.ReadToEnd();//读取全部字符,并保存到htmlCode成员变量
sr.Close();
return true;
}
catch(IOException)//捕捉IO异常
{
return false;
}
}
public void fill(string var, string content)//把内容替换模板中的“变量”
{
htmlCode = htmlCode.Replace(var, content);
}
public bool write(string outPath)//把HTML文件保存到磁盘
{
try
{
string fileRealPath = System.Web.HttpContext.Current.Server.MapPath(outPath);
StreamWriter sw = new StreamWriter(fileRealPath, false, code);
sw.Write(htmlCode);//把HTML文件写到磁盘上
sw.Flush();
sw.Close();
return true;
}
catch (IOException)//捕捉IO异常
{
return false;
}
}
}