用ASP.NET,C#實現產生靜態頁面

來源:互聯網
上載者:User
示列代碼    
   
  1.定義(template.htm)html模板頁面    
   
  <html>    
   
  <head>    
   
  <title></title>    
   
  <meta   http-equiv="Content-Type"   content="text/html;   charset=gb2312">    
   
  </head>    
   
  <body   >    
   
  <table   $htmlformat[0]   height="100%"   border="0"   width="100%"   cellpadding="10"   cellspacing="0"   bgcolor="#eeeeee"   style="border:1px   solid   #000000">    
   
  <tr>    
   
  <td   width="100%"   valign="middle"   align="left">    
   
  <span   style="color:   $htmlformat[1];font-size:   $htmlformat[2]">$htmlformat[3]</span>    
   
  </td>    
   
  </tr>    
   
  </table>    
   
  </body>    
   
  </html>    
   
  2.asp.net代碼:    
   
  //---------------------讀html模板頁面到stringbuilder對象裡----    
   
  string[]   format=new   string[4];//定義和htmlyem標記數目一致的數組    
   
  StringBuilder   htmltext=new   StringBuilder();    
   
  try    
   
  {    
   
  using   (StreamReader   sr   =   new   StreamReader("存放模板頁面的路徑和頁面名"))    
   
  {    
   
  String   line;    
   
  while   ((line   =   sr.ReadLine())   !=   null)    
   
  {    
   
  htmltext.Append(line);    
   
  }    
   
  sr.Close();    
   
  }    
   
  }    
   
  catch    
   
  {    
   
  Response.Write("<Script>alert('讀取檔案錯誤')</Script>");    
   
  }    
   
  //---------------------給標記數組賦值------------    
   
  format[0]="background=\"bg.jpg\"";//背景圖片    
   
  format[1]=   "#990099";//字型顏色    
   
  format[2]="150px";//字型大小    
   
  format[3]=   "<marquee>產生的模板html頁面</marquee>";//文字說明    
   
  //----------替換htm裡的標記為你想加的內容    
   
  for(int   i=0;i<4;i++)    
   
  {    
   
  htmltext.Replace("$htmlformat["+i+"]",format[i]);    
   
  }    
   
  //----------產生htm檔案------------------――    
   
  try    
   
  {    
   
  using(StreamWriter   sw=new   StreamWriter("存放路徑和頁面名",false,System.Text.Encoding.GetEncoding("GB2312")))    
   
  {    
   
  sw.WriteLine(htmltext);    
   
  sw.Flush();    
   
  sw.Close();    
   
  }    
   
  }    
   
  catch    
   
  {    
   
  Response.Write   ("The   file   could   not   be   wirte:");    
   
  }    
   
  小結    
   
  用此方法可以方便的產生html檔案。程式使用了是迴圈替換,因此對需替換大量元素的模板速度非常快。
----------------------------------------------------------------
方法2:
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>  
   
   
   
   
   
   
    biaoti  
    <br>  
    content<br>  
    author  
    </body>  
  </HTML>  
   
   
   
   
   
   
    biaoti  
    <br>  
    content<br>  
    author  
    </body>  
  </HTML>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.