欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET編程簡單實現(xiàn)生成靜態(tài)頁面的方法【附demo源碼下載】

 更新時間:2017年07月18日 12:08:19   作者:ForEvErNoMe  
這篇文章主要介紹了ASP.NET編程簡單實現(xiàn)生成靜態(tài)頁面的方法,較為詳細(xì)的分析了asp.net生成靜態(tài)頁面的步驟與相關(guān)操作技巧,并附帶相關(guān)實例源碼供讀者下載參考,需要的朋友可以參考下

本文實例講述了ASP.NET編程簡單實現(xiàn)生成靜態(tài)頁面的方法。分享給大家供大家參考,具體如下:

1. 使用場景

當(dāng)頁面的數(shù)據(jù)不需要經(jīng)常更改時可采用靜態(tài)頁面方式。

2. 使用靜態(tài)頁面的好處

(1)提高網(wǎng)站的訪問速度

(2)減輕服務(wù)器負(fù)擔(dān)

(3)利于搜索引擎抓取

3. ASP.NET生成靜態(tài)頁面

生成靜態(tài)頁面方法有很多種,先說下我使用的其中的一種。參考資料

基本思路:

(1)創(chuàng)建模板template.html文件,在里面定義一些特殊的字符串格式用于替換內(nèi)容,如$htmlformat

(2)讀取模板,賦值到StringBuilder對象中

(3)將特殊的字符串格式替換為你想要的內(nèi)容

(4)創(chuàng)建新的靜態(tài)頁面,并將StringBuilder對象寫入到文件中即可

4. 方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
/// <summary>
///ConvertHtmlPage 生成靜態(tài)頁面
/// </summary>
public class ConvertHtmlPage
{
  /// <summary>
  /// 生成HTML文件
  /// </summary>
  /// <param name="templatePath">模板路徑</param>
  /// <param name="templateName">模板名稱</param>
  /// <param name="htmlPath">生成HTML的路徑</param>
  /// <param name="htmlName">生成HTML的名稱</param>
  /// <param name="format">替換的內(nèi)容</param>
  /// <returns></returns>
  public static bool CreatePage(string templatePath,string templateName, string htmlPath, string htmlName,List<string> format)
  {
    try
    {
      //讀取模板文件
      StringBuilder htmltext = new StringBuilder();
      using (StreamReader sr = new StreamReader(templatePath+templateName))
      {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
          htmltext.AppendLine(line);
        }
        sr.Close();
      }
      //替換HTML中的標(biāo)記內(nèi)容
      for (int i = 0; i < format.Count; i++)
      {
        htmltext.Replace("$htmlformat[" + i + "]", format[i]);
      }
      //生成HTML文件
      using (StreamWriter sw = new StreamWriter(htmlPath+htmlName, false, System.Text.Encoding.GetEncoding("GB2312")))
      {
        sw.WriteLine(htmltext);
        sw.Flush();
        sw.Close();
      }
    }
    catch (Exception ex)
    {
      return false;
    }
    return true;
  }
}

附:DEMO實例點擊此處本站下載。

更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net文件操作技巧匯總》、《asp.net操作json技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。

希望本文所述對大家asp.net程序設(shè)計有所幫助。

相關(guān)文章

最新評論