国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

[收藏]ASP.Net生成靜態(tài)HTML頁(yè) 選擇自 coofucoo 的 Blog

2019-11-17 05:26:33
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
 環(huán)境:Microsoft .NET Framework SDK v1.1
OS:Windows Server 2003 中文版
asp.net生成靜態(tài)Html頁(yè)
在ASP中實(shí)現(xiàn)的生成靜態(tài)頁(yè)用到的FileSystemObject對(duì)象!
在.Net中涉及此類(lèi)操作的是System.IO
以下是程序代碼 注:此代碼非原創(chuàng)!參考別人代碼
//生成HTML頁(yè)
  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";
   // 替換內(nèi)容
   // 這時(shí),模板文件已經(jīng)讀入到名稱(chēng)為str的變量中了
   str =str.Replace("ShowArticle",strText); //模板頁(yè)中的ShowArticle
   str = str.Replace("biaoti",strText);
   str = str.Replace("content",strContent);
   str = str.Replace("author",strAuthor);
   // 寫(xiě)文件
   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;

此函數(shù)放在Conn.CS基類(lèi)中了
在添加新聞的代碼中引用 注:工程名為Hover
 
    if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
    {
     Response.Write("添加成功");
    }
    else
    {
     Response.Write("生成HTML出錯(cuò)!");
    }
-------------------------------------------------------------------------
模板頁(yè)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>

 biaoti
 <br>
 content<br>
 author
 </body>
</HTML>

------------------------------------------------------------------------
提示添加成功后會(huì)出以當(dāng)前時(shí)間為文件名的html文件!上面只是把傳遞過(guò)來(lái)的幾個(gè)參數(shù)直接寫(xiě)入了HTML文件中,在實(shí)際應(yīng)用中需要先添加數(shù)據(jù)庫(kù),然后再寫(xiě)入HTML文件
而且需要把生成的文件名等寫(xiě)入數(shù)庫(kù)以便以后調(diào)用等,此實(shí)例只是實(shí)現(xiàn)了根據(jù)提交過(guò)來(lái)參數(shù)替換模板中的相應(yīng)的字段! 需要完善的地方很多!哪位有高見(jiàn),歡迎賜教!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 土默特左旗| 恭城| 灵石县| 朔州市| 红河县| 塔河县| 桦川县| 华容县| 富蕴县| 昌宁县| 宝丰县| 荃湾区| 格尔木市| 安溪县| 建始县| 丹棱县| 伊吾县| 沧州市| 尼勒克县| 广南县| 海口市| 青冈县| 南部县| 梨树县| 湾仔区| 汉源县| 凯里市| 乌兰县| 叶城县| 泸州市| 库伦旗| 苍山县| 高平市| 安庆市| 突泉县| 龙山县| 梨树县| 肥东县| 宕昌县| 郓城县| 万州区|