不知道大家注意到了沒有,Google和Baidu網頁的HTML源代碼是混合在一起的。HTML代碼混合在一起,出發(fā)點是為了減小網頁體積,從而加快網頁加載速度。
寫個函數(shù)把網頁HTML源代碼的換行符和空格過濾掉其實并不難,我這里是寫了個基類,在asp.net編程時,頁面只要繼承這個基類,那么輸出的HTML代碼就會自動去掉換行符,和多余的空格符號,例如“> <”之間的空格符號。
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 頁面基類
/// </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);
}
}