国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 凤台县| 仁寿县| 兰西县| 临海市| 南召县| 平陆县| 耿马| 平凉市| 沙坪坝区| 会昌县| 关岭| 庆元县| 大关县| 安阳市| 通山县| 德钦县| 铜川市| 大邑县| 云浮市| 沅陵县| 荆门市| 呼玛县| 松溪县| 沂南县| 当涂县| 资阳市| 土默特右旗| 黄梅县| 横峰县| 山阳县| 彰武县| 石泉县| 德令哈市| 安乡县| 潞西市| 蒲江县| 大理市| 桑日县| 晴隆县| 朔州市| 东明县|