Asp.net產生靜態頁面原理

來源:互聯網
上載者:User
 

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

namespace menutest
{
 /// <summary>
 /// Conn 的摘要說明。
 /// </summary>
 // by kyo
 // 此類是產生靜態網頁的小程式 
 public class Conn
 {
  public Conn()
  {
  
  }
  public static bool WriteFile(string strText,string strContent,string strAuthor)
  {
   string path = HttpContext.Current.Server.MapPath("/menutest/");
   Encoding code = Encoding.GetEncoding("gb2312");
   // 讀模數板檔案
   string temp = HttpContext.Current.Server.MapPath("/menutest/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";
   string htmlfilename="kyo.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;
  }
 }
}
//原理是利用System.IO中的類讀寫模板檔案,然後用Replace替換掉模板中的標籤,寫入靜態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.