不知你是否留意過,有一些網(wǎng)站的html代碼都是混在一起,沒有任何空格和換行等多余字符。它的好處不用多說——界面大小絕對優(yōu)化?;蛟S您在想,他們這樣做大大降低了可讀性。的確,我們看是很亂,只能借用第三方軟件重新布局代碼。但是,我想他們開發(fā)時使用的源碼不可能是混一團(tuán),前不久發(fā)現(xiàn)一個頁面基類,大概可以解釋這個問題,不多說,看源碼:
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.WebControls.WebParts;
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 v]“, “”);
html = Regex.Replace(html, ” {2,}”, ” “);
html = Regex.Replace(html, “>[ ]{1}”, “>”);
writer.Write(html);
}
}