乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      c#生成靜態(tài)html文件,封裝類

       昵稱10504424 2014-01-16

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

      幸運(yùn), 驚喜, 智慧, 美好!歡迎加入軟件技術(shù)學(xué)習(xí)群260594650
      復(fù)制代碼
       1 public abstract class StaticBase : IDisposable
       2     {
       3         /// <summary>
       4         /// 默認(rèn)編碼方式
       5         /// </summary>
       6         protected Encoding code = Encoding.GetEncoding("utf-8");
       7         /// <summary>
       8         /// 寫入頁面數(shù)據(jù)流
       9         /// </summary>
      10         protected StreamWriter sw = null;
      11         /// <summary>
      12         /// 讀取頁面數(shù)據(jù)流
      13         /// </summary>
      14         protected StreamReader sr = null;
      15         /// <summary>
      16         /// 生成的靜態(tài)頁面保存文件夾路徑
      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         /// 具體生成靜態(tài)方法
      27         /// </summary>
      28         protected abstract bool WriteFile();
      29         /// <summary>
      30         /// 不同模塊的文件名稱
      31         /// </summary>
      32         protected Dictionary<FlagsFileName, string> FileName
      33         {
      34             get
      35             {
      36                 return new Dictionary<FlagsFileName, string>
      37                 {
      38                     {FlagsFileName.News,"article"},
      39                     {FlagsFileName.head,"head"},
      40                     {FlagsFileName.foot,"foot"},
      41                 };
      42             }
      43         }
      44        // http://www.cnblogs.com/roucheng/
      45         #region IDisposable 成員
      46 
      47         public void Dispose()
      48         {
      49             sw.Dispose();
      50             sr.Dispose();
      51         }
      52 
      53         #endregion
      54     }
      55     #region 對(duì)應(yīng)的頁面名稱
      56     /// <summary>
      57     /// 對(duì)應(yīng)的頁面名稱
      58     /// </summary>
      59     public enum FlagsFileName : byte
      60     {
      61         /// <summary>
      62         /// 新聞
      63         /// </summary>
      64         [Description("新聞")]
      65         News = 0,
      66         /// <summary>
      67         /// 頭部
      68         /// </summary>
      69         [Description("頭部")]
      70         head=1,
      71         /// <summary>
      72         /// 腳部
      73         /// </summary>
      74         [Description("腳部")]
      75         foot=2,
      76     }
      復(fù)制代碼

      最后的一個(gè)枚舉用于定義不同位置或不同類別的靜態(tài)頁所對(duì)應(yīng)的子類

      ,接下來看看其中一個(gè)子類的實(shí)現(xiàn)(該子類是用于所有單頁,如數(shù)據(jù)庫中有100條新聞?dòng)涗?,那相?yīng)的生成100個(gè)新聞html頁面,格式用模板定義的格式確定)

      首先模板文件時(shí)靜態(tài)的html頁面,其中所有的需要從數(shù)據(jù)庫中替換的字段用一對(duì)$包含,如數(shù)據(jù)庫中的新聞標(biāo)題字段為titles,則模板頁中相應(yīng)的標(biāo)題位置用$titles$表示,頁面如下

      復(fù)制代碼
       1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
       2 <html xmlns="http://www./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"> </td>
      26   </tr>
      27     <tr>
      28       <td class="ri_mid" id="mid_box"> </td>
      29     </tr>
      30     <tr>
      31       <td height="44" class="ri_bottom"> </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>
      復(fù)制代碼

      http://www.cnblogs.com/roucheng/

      到這里知道個(gè)大概了吧,接下來就是這中頁面類型的子類實(shí)現(xiàn),我將它的名稱定義為ViewPage,因?yàn)樗锌梢詥为?dú)顯示的頁面都可以用這個(gè)子類,代碼如下

      復(fù)制代碼
        1 public class ViewPage : StaticBase
        2     {
        3         /// <summary>
        4         /// 是否操作成功
        5         /// </summary>
        6         private bool o_sucess = true;
        7         /// <summary>
        8         /// 錯(cuò)誤信息
        9         /// </summary>
       10         private string errorstring = string.Empty;
       11         /// <summary>
       12         /// 模板文件名稱
       13         /// </summary>
       14         private string masterhtml;
       15         /// <summary>
       16         /// 數(shù)據(jù)源 
       17         /// </summary>
       18         private IEnumerable<DataRow> rowlist;
       19         /// <summary>
       20         /// 模塊類別
       21         /// </summary>
       22         private FlagsFileName fname;
       23         /// <summary>
       24         /// 指定命名文件的標(biāo)志列(從數(shù)據(jù)庫中的字段)
       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         /// 構(gòu)造器靜態(tài)生成對(duì)象
       39         /// </summary>
       40         /// <param name="rlist">需要生成靜態(tài)文件的數(shù)據(jù)源</param>
       41         /// <param name="fn">文件類別枚舉</param>
      
       42         /// <param name="myid">此字段為數(shù)據(jù)庫表中字段,由該字段指定生成的文件名字標(biāo)志 </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)//異常則指定返回的錯(cuò)誤信息 
       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)//遍歷數(shù)據(jù)源數(shù)組中的每個(gè)數(shù)據(jù)表
       78             {
       79                 string newString = str;
       80                 try
       81                 {
       82                     htmlfilename = FileName[fname] + "_" + row[thekey].ToString() + ".html";//給文件命名
       83                     foreach (DataColumn c in row.Table.Columns)//遍歷單個(gè)數(shù)據(jù)表的列名
       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     }
      復(fù)制代碼

      好,到這里實(shí)現(xiàn)了底層的思路設(shè)計(jì),那調(diào)用就很簡(jiǎn)單了,某個(gè)aspx頁面,一個(gè)按鈕button,一個(gè)點(diǎn)擊事件Button_Click,點(diǎn)擊事件內(nèi)需要做的就是聲明一個(gè)基類StaticBase,將它實(shí)例化成一個(gè)子類ViewPage,傳遞的參數(shù)為一個(gè)數(shù)據(jù)項(xiàng)集合,DataRow[]為從數(shù)據(jù)表中讀取的集合,包含需要替換的字段,如select titles,contens,id from news(從新聞表中獲得標(biāo)識(shí)id,標(biāo)題,內(nèi)容),以及類型FlagsFileName.News為前天基類提到過的枚舉類型,為單獨(dú)頁面的生成方式,已經(jīng)重命名的標(biāo)識(shí)列,如此處為id,則生成的頁面格式為

      news_1.html,news_2.html以此類推,代碼如下

      復(fù)制代碼
       1 protected void Create_Click(object sender, EventArgs e)
       2         {
       3             IEnumerable<DataRow> rowlist = TNotice_Command.SelectTNotice(-1);
       4             using (StaticBase sb = new ViewPage((DataRow[])rowlist, FlagsFileName.News, "NID"))
       5             {
       6                 if (!sb.Osucess)
       7                 {
       8                     Response.Write("<script language=javascript>alert('" + sb.Errorstring + "')</script>");
       9                 }
      10             }
      11         }
      復(fù)制代碼

      看到這里大家如果再從頭看一遍,相信就能知道靜態(tài)文件的生成的原理了,接下來研究如果生成分頁頁面的靜態(tài)文件,文章內(nèi)容簡(jiǎn)單,但希望能大家一點(diǎn)思路。

      分類: C#

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多