首页 > 代码库 > asp.net页面过滤所有换行符和多余空格
asp.net页面过滤所有换行符和多余空格
写个函数把网页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);
}
}
asp.net页面过滤所有换行符和多余空格