ASP.NET產生靜態頁面方法(三種)

來源:互聯網
上載者:User

第一種方法:向伺服器的動態網頁面發送請求,擷取頁面的html代碼。這種方法缺點顯而易見:速度慢。另外如果請求的動態網頁面有驗證控制項的話,返回的html頁面卻無法進行資料驗證。但這種方法寫起來比較簡單。主要代碼如下:

#region//產生被請求URL靜態頁面

public static void getUrltoHtml(string Url,string Path)//Url為動態網頁面地址,Path為產生的靜態頁面

{

try

{

   System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);

     // Get the response instance.

   System.Net.WebResponse wResp =wReq.GetResponse();

     // Get the response stream.

   System.IO.Stream respStream = wResp.GetResponseStream();

     // Dim reader As StreamReader = New StreamReader(respStream)

   System.IO.StreamReader reader = new System.IO.StreamReader(respStream,System.Text.Encoding.GetEncoding("gb2312"));

   string str=reader.ReadToEnd();

   System.IO.StreamWriter sw=new System.IO.StreamWriter(Path,false,System.Text.Encoding.GetEncoding("gb2312"));

   sw.Write(str);

   sw.Flush();

   sw.Close();

   System.Web.HttpContext.Current.Response.Write("<script>alert('頁面產生成功!');</script>");

}

catch(System.Exception ex)

{

   System.Web.HttpContext.Current.Response.Write("<script>alert('頁面產生失敗!"+ex.Message+"');</script>");

}

}

#endregion

第二種方法:從檔案讀模數版,替換模版中的參數後輸出檔案,這種方法的產生速度上比第一種要快許多,而且模版內容可以用工具任意編輯

主要代碼:

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 xinxi

{

/// <summary>

/// CreatePage的摘要說明。

/// </summary>

// www.365xinxi.net

// 此類是產生靜態網頁的小程式

public class Create

{

   public void CreatePage()

   {


   }

   public static bool WriteFile(string strText,string strContent,string strAuthor)

   {

    string path = HttpContext.Current.Server.MapPath("/test/");//檔案輸出目錄

    Encoding code = Encoding.GetEncoding("gb2312");

    // 讀模數板檔案

    string temp = HttpContext.Current.Server.MapPath("/template/test.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;

   }

}

}

//原理是利用System.IO中的類讀寫模板檔案,然後用Replace替換掉模板中的標籤,寫入靜態html

第三種方法:如果產生的檔案數量比較多,第二種方法就要反覆讀模數版內容,這時可以用第三種方法——直接將你的模版寫在代碼中,和上次我寫的網站Header和Footer的製作方法類似:

using System;

using System.Collections;

using System.Data;

using System.Data.OleDb;

using System.Text;

using System.IO;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

namespace xinxi

{

/// <summary>

/// 自訂公用函數

/// </summary>

public class myfun

{

   #region//定義模版頁

   public static string SiteTemplate()

   {

    string str="";

    str+="...";//模版頁html代碼

    return str;

   }

   #endregion

   public static bool WriteFile(string strText,string strContent,string strAuthor)

   {

    string path = HttpContext.Current.Server.MapPath("/test/");//檔案輸出目錄

    Encoding code = Encoding.GetEncoding("gb2312");

    StreamWriter sw=null;

    string str=SiteTemplate();//讀模數版頁面html代碼

    string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";//靜態檔案名稱

    // 替換內容

    str = str.Replace("ShowArticle",strText);

    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;

   }



}

}

三種方法比較起來產生速度由慢到快,易操作性則由簡到繁。還請根據實際情況選擇合適的方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.