May 9, 2006 A couple a months ago, I wrote how to remove
whitespace from an ASP.NET page. However, the whitespace removal was to extreme
for most websites. It mangled with the HTML to such an extent that only very simple
websites could use it without damaging their code.
I’ve just finished a more complex website and therefore had to make some changes to
my original whitespace removal method. Otherwise, the JavaScript wouldn’t work including
the postbacks.
To make it work, simply paste this method into your .aspx or master page.
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,3}(?=[<])|(?<=[>])\s{2,3}(?=[<])|(?=[\n])\s{2,3}", String.Empty);
html = Regex.Replace(html, @"[\f\r\t\v]?([\n\xFE\xFF/{}[\];,<>*%&|^!~?:=])[\f\r\t\v]?", "$1");
html = html.Replace(";\n", ";");
writer.Write(html.Trim());
}
}
I’ve calculated that the total HTML file size will be reduces by 10-13 %. To see an
example of this method, just visit www.wulffmorgenthaler.com and
view the source.
* $4.95/month BlogEngine.net Hosting – Click Here!