我正在使用以下代码来缩小从aspx页面运行时生成的html。
protected override void Render(HtmlTextWriter writer)
{
TextWriter output = new StringWriter();
base.Render(new HtmlTextWriter(output));
String html = output.ToString();
html = Regex.Replace(html, @"\n|\t", " ");
html = Regex.Replace(html, @">\s+<", "><").Trim();
html = Regex.Replace(html, @"\s{2,}", " ");
writer.Write(html);
}
是否有更好的方法来做同样的事情。
非常感谢你。
答案 0 :(得分:2)
是的,使用Html Tidy甚至是HTTP Module。
答案 1 :(得分:1)
protected override void Render(HtmlTextWriter writer)
{
using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))
{
base.Render(htmlwriter);
string html = htmlwriter.InnerWriter.ToString();
html = Regex.Replace(html, @"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}", "");
html = Regex.Replace(html, @"[ \f\r\t\v]?([\n\xFE\xFF/{}[\];,<>*%&|^!~?:=])[\f\r\t\v]?", "$1");
html = html.Replace(";\n", ";");
writer.Write(html);
}
}