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

首頁 > 學院 > 開發設計 > 正文

c#生成靜態html文件,封裝類

2019-11-17 03:24:07
字體:
來源:轉載
供稿:網友

c#生成靜態html文件,封裝類

由于這段時間比較輕松,于是想到很多的企業網站,新聞網站需要將頁面靜態化,于是寫了個封裝類來實現靜態文件的生成,思路比較簡單,但未完善,網友可根據自己的思路將此類擴展,運用了簡單工廠模式(本來剛開始看設計模式,是個好書),好了,廢話不多說,先來看看靜態類的父類:StaticBase(抽象類)

 1 public abstract class StaticBase : IDisposable 2     { 3         /// <summary> 4         /// 默認編碼方式 5         /// </summary> 6         PRotected Encoding code = Encoding.GetEncoding("utf-8"); 7         /// <summary> 8         /// 寫入頁面數據流 9         /// </summary>10         protected StreamWriter sw = null;11         /// <summary>12         /// 讀取頁面數據流13         /// </summary>14         protected StreamReader sr = null;15         /// <summary>16         /// 生成的靜態頁面保存文件夾路徑17         /// </summary>18         protected string SavePath = "/Default/";19         /// <summary>20         /// 模板頁面的文件夾路徑21         /// </summary>22         protected string PagePath = "/Master/";23         public abstract bool Osucess { set; get; }24         public abstract string Errorstring { set; get; }25         /// <summary>26         /// 具體生成靜態方法27         /// </summary>28         protected abstract bool WriteFile();29         /// <summary>30         /// 不同模塊的文件名稱31         /// </summary>32         protected Dictionary<FlagsFileName, string> FileName33         {34             get35             {36                 return new Dictionary<FlagsFileName, string>37                 {38                     {FlagsFileName.News,"article"},39                     {FlagsFileName.head,"head"},40                     {FlagsFileName.foot,"foot"},41                 };42             }43         }44        // http://m.survivalescaperooms.com/roucheng/45         #region IDisposable 成員46 47         public void Dispose()48         {49             sw.Dispose();50             sr.Dispose();51         }52 53         #endregion54     }55     #region 對應的頁面名稱56     /// <summary>57     /// 對應的頁面名稱58     /// </summary>59     public enum FlagsFileName : byte60     {61         /// <summary>62         /// 新聞63         /// </summary>64         [Descr

最后的一個枚舉用于定義不同位置或不同類別的靜態頁所對應的子類

,接下來看看其中一個子類的實現(該子類是用于所有單頁,如數據庫中有100條新聞記錄,那相應的生成100個新聞html頁面,格式用模板定義的格式確定)

首先模板文件時靜態的html頁面,其中所有的需要從數據庫中替換的字段用一對$包含,如數據庫中的新聞標題字段為titles,則模板頁中相應的標題位置用$titles$表示,頁面如下

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>$Titles$</title> 6 </script> 7 </head> 8 <body> 9 <div id="wrap">10   $head$11   <!--hd end-->12   <div class="clear"></div>13   <div id="wp">14   <table border="0" cellpadding="0" cellspacing="0" width="980">15     <tr>16       <td rowspan="3" valign="top" id="main_box" class="index_box2">17       <div class="subtitle subtitle4"></div>18       <div class="article">19       <div class="title">$Titles$</div>20          $Contents_tw$21       </div> 22         23        24       </td>25       <td width="48" height="44" class="ri_top">&nbsp;</td>26   </tr>27     <tr>28       <td class="ri_mid" id="mid_box">&nbsp;</td>29     </tr>30     <tr>31       <td height="44" class="ri_bottom">&nbsp;</td>32     </tr>33 </table>34   </div>35   <!--wp end-->36 </div>37 <!--wrap end-->38 39 $foot$40 <!--ft end-->41 </body>42 </html>

http://m.survivalescaperooms.com/roucheng/

到這里知道個大概了吧,接下來就是這中頁面類型的子類實現,我將它的名稱定義為ViewPage,因為所有可以單獨顯示的頁面都可以用這個子類,代碼如下

  1 public class ViewPage : StaticBase  2     {  3         /// <summary>  4         /// 是否操作成功  5         /// </summary>  6         private bool o_sucess = true;  7         /// <summary>  8         /// 錯誤信息  9         /// </summary> 10         private string errorstring = string.Empty; 11         /// <summary> 12         /// 模板文件名稱 13         /// </summary> 14         private string masterhtml; 15         /// <summary> 16         /// 數據源  17         /// </summary> 18         private IEnumerable<DataRow> rowlist; 19         /// <summary> 20         /// 模塊類別 21         /// </summary> 22         private FlagsFileName fname; 23         /// <summary> 24         /// 指定命名文件的標志列(從數據庫中的字段) 25         /// </summary> 26         private string thekey; 27         public override bool Osucess 28         { 29             get { return o_sucess; } 30             set { o_sucess = value; } 31         } 32         public override string Errorstring 33         { 34             get { return errorstring; } 35             set { errorstring = value; } 36         } 37         /// <summary> 38         /// 構造器靜態生成對象 39         /// </summary> 40         /// <param name="rlist">需要生成靜態文件的數據源</param> 41         /// <param name="fn">文件類別枚舉</param> 42         /// <param name="myid">此字段為數據庫表中字段,由該字段指定生成的文件名字標志 </param> 43         public ViewPage(DataRow[] rlist,FlagsFileName fn,string myid) 44         { 45             this.thekey = myid; 46             this.fname = fn; 47             this.rowlist = rlist; 48             this.masterhtml = FileName[fn] + ".html"; 49             WriteFile(); 50         } 51  52         protected override bool WriteFile() 53         { 54             string str = ""; 55             try//從指定模板文件中讀取html代碼 56             { 57                 sr = new StreamReader(HttpContext.Current.Server.MapPath(PagePath + this.masterhtml), code); 58                 str = sr.ReadToEnd(); 59             } 60             catch (Exception ex)//異常則指定返回的錯誤信息  61             { 62                 sr.Close(); 63                 sr.Dispose(); 64                 this.o_sucess = false; 65                 this.errorstring = ex.Message; 66                 return this.o_sucess; 67             } 68             sr.Close(); 69             sr.Dispose(); 70             List<FlagsFileName> fn = new List<FlagsFileName>(); 71             fn.Add(FlagsFileName.head); 72             fn.Add(FlagsFileName.foot); 73             PointPage pg = new PointPage(fn, str); 74             //要保存的文件名稱 75             string htmlfilename = string.Empty; 76             string changestring = "";//要更改的字符串 77             foreach (DataRow row in this.rowlist)//遍歷數據源數組中的每個數據表 78             { 79                 string newString = str; 80                 try 81                 { 82                     htmlfilename = FileName[fname] + "_" + row[thekey].ToString() + ".html";//給文件命名 83                     foreach (DataColumn c in row.Table.Columns)//遍歷單個數據表的列名 84                     { 85                         changestring = "$" + c.ColumnName + "$"; 86                         newString = newString.Replace(changestring, row[c].ToString()); 87                     } 88                     sw = new StreamWriter(HttpContext.Current.Server.MapPath(SavePath + htmlfilename), false, code); 89                     sw.Write(newString); 90                     sw.Flush(); 91                 } 92                 catch (Exception ex) 93                 { 94                     this.o_sucess = false; 95                     this.errorstring = ex.Message; 96                     return this.o_sucess; 97                 } 98  99             }100             sw.Dispose();101             sw.Close();102             return true;103         }104     }

好,到這里實現了底層的思路設計,那調用就很簡單了,某個aspx頁面,一個按鈕button,一個點擊事件Button_Click,點擊事件內需要做的就是聲明一個基類StaticBase,將它實例化成一個子類ViewPage,傳遞的參數為一個數據項集合,DataRow[]為從數據表中讀取的集合,包含需要替換的字段,如select titles,c

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兰西县| 蛟河市| 平谷区| 彩票| 布尔津县| 永春县| 泉州市| 桃源县| 连州市| 龙州县| 仪征市| 文水县| 荣成市| 安远县| 辉县市| 弥渡县| 平定县| 霸州市| 广德县| 江华| 象州县| 平南县| 桂东县| 濮阳县| 玛曲县| 南丰县| 邹平县| 英德市| 永和县| 襄城县| 界首市| 长泰县| 饶阳县| 明星| 哈尔滨市| 图们市| 武城县| 仪陇县| 措美县| 麻江县| 安平县|