不知道大家注意到了沒有,Google和Baidu網(wǎng)頁(yè)的HTML源代碼是混合在一起的。HTML代碼混合在一起,出發(fā)點(diǎn)是為了減小網(wǎng)頁(yè)體積,從而加快網(wǎng)頁(yè)加載速度。
寫個(gè)函數(shù)把網(wǎng)頁(yè)HTML源代碼的換行符和空格過(guò)濾掉其實(shí)并不難,我這里是寫了個(gè)基類,在asp.net編程時(shí),頁(yè)面只要繼承這個(gè)基類,那么輸出的HTML代碼就會(huì)自動(dòng)去掉換行符,和多余的空格符號(hào),例如“> <”之間的空格符號(hào)。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.IO;
/// <summary>
/// PageBase 頁(yè)面基類
/// </summary>
public class PageBase : System.Web.UI.Page
{
protected override void Render(HtmlTextWriter writer)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
base.Render(htmlWriter);
string html = sw.ToString();
html = Regex.Replace(html, "[\f\n\r\t\v]", "");
html = Regex.Replace(html, " {2,}", " ");
html = Regex.Replace(html, ">[ ]{1}", ">");
writer.Write(html);
}
}