ASP.Net生成静态HTML页!
環境:Microsoft .NET Framework SDK v1.1
OS:Windows Server 2003 中文版
ASP.Net生成靜態HTML頁
在Asp中實現的生成靜態頁用到的FileSystemObject對象!
在.Net中涉及此類操作的是System.IO
//生成HTML頁
??public static bool WriteFile(string strText,string strContent,string strAuthor)
??{
???string path = HttpContext.Current.Server.MapPath("/news/");
???Encoding code = Encoding.GetEncoding("gb2312");
???// 讀取模板文件
???string temp = HttpContext.Current.Server.MapPath("/news/text.html");
???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 htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
???// 替換內容
???// 這時,模板文件已經讀入到名稱為str的變量中了
???str =str.Replace("ShowArticle",strText); //模板頁中的ShowArticle
???str = str.Replace("biaoti",strText);
???str = str.Replace("content",strContent);
???str = str.Replace("author",strAuthor);
???// 寫文件
???try
???{
????sw = new StreamWriter(path + htmlfilename , false, code);
????sw.Write(str);
????sw.Flush();
???}
???catch(Exception ex)
???{
????HttpContext.Current.Response.Write(ex.Message);
????HttpContext.Current.Response.End();
???}
???finally
???{
????sw.Close();
???}
???return true;
此函數放在Conn.CS基類中了
在添加新聞的代碼中引用 注:工程名為Hover
?
????if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
????{
?????Response.Write("添加成功");
????}
????else
????{
?????Response.Write("生成HTML出錯!");
????}
-------------------------------------------------------------------------
模板頁Text.html代碼
-------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
?<HEAD>
??<title>ShowArticle</title>
??
?<body>
?biaoti
?<br>
?content<br>
?author
?</body>
</HTML>
?
------------------------------------------------------------------------
提示添加成功后會出以當前時間為文件名的html文件!上面只是把傳遞過來的幾個參數直接寫入了HTML文件中,在實際應用中需要先添加數據庫,然后再寫入HTML文件
而且需要把生成的文件名等寫入數庫以便以后調用等,此實例只是實現了根據提交過來參數替換模板中的相應的字段! 需要完善的地方很多!哪位有高見,歡迎賜教!
總結
以上是生活随笔為你收集整理的ASP.Net生成静态HTML页!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【软件质量】对this或字符串加锁的弊病
- 下一篇: 【系统设计】统一过程的类抽取