這篇文章將為大家詳細講解有關(guān)ASP.NET編程中如何實現(xiàn)生成靜態(tài)頁面,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

具體如下:
1. 使用場景
當頁面的數(shù)據(jù)不需要經(jīng)常更改時可采用靜態(tài)頁面方式。
2. 使用靜態(tài)頁面的好處
(1)提高網(wǎng)站的訪問速度
(2)減輕服務(wù)器負擔
(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中的標記內(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;
}
}關(guān)于“ASP.NET編程中如何實現(xiàn)生成靜態(tài)頁面”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
文章名稱:ASP.NET編程中如何實現(xiàn)生成靜態(tài)頁面-創(chuàng)新互聯(lián)
本文路徑:http://chinadenli.net/article28/ceoscp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、服務(wù)器托管、ChatGPT、動態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容