Remove whitespace from your ASP.NET page

by Mads Kristensen 8. February 2006 00:12

In a normal webpage there is a lot of whitespace. We put it there in form of tabs and carriage-returns in order to make the document more maintainable and easier to read. But it has its price in terms of adding to the overall weight of the document in kilobytes and it takes longer for the browser to render a page with a lot of whitespace. This is especially true for IE6, which renders whitespace a lot slower than its counterparts.

The problem is, we don’t want to write html without whitespace. It would make it impossible to maintain. What we are looking for is an automatic way to filter the whitespace at runtime. In ASP.NET this is easy. You can override the page’s Render method and do the filtering in there. We also want to be able to turn the filtering on and off easily, because when we develop we often want to look at the rendered html code and make sense of it.

Here is an example that does just that. It overrides the render method of the page and is turned on/off in the web.config.

Implement this on the aspx page or even better on the master page if you use ASP.NET 2.0:

//Add this to the top of the page
    using
System.Configuration;
    using System.Web.UI;
    using System.Text.RegularExpressions;

//Overrides the Render method
    protected override void Render(HtmlTextWriter writer)
    {
        using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))
        {
            base.Render(htmlwriter);
            string html = htmlwriter.InnerWriter.ToString();

            if ((ConfigurationManager.AppSettings.Get("RemoveWhitespace") + string.Empty).Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                html = Regex.Replace(html, @"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}", 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());
        }
    }

and add this to the web.config appsetting section:

<add key="RemoveWhitespace" value="true"/>

It's as easy as that and the overhead is reasonable. I use this on the most of the websites I build and have never noticed any negative impact in any way.

Update, May 4th 2006
Some have had their problems with this method, complaining that it screws up the HTML and embedded JavaScript. I've modified the method to address these issues and created a safe whitespace removal method.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags:

ASP.NET

Comments

2/19/2006 6:15:51 PM #

Ady

Very nice, unfortunatly it doesn't work to great with multiline text boxes, If the user has entered multiple line feeds, they are removed (in postback).

Ady |

2/20/2006 4:39:40 AM #

 Mads Kristensen

Hi Ady.
Of course you're right about multiline textboxes. You would have to turn the filtering off at pages with those textboxes. It could be done automatically if the regular expression didn't filter text from within textarea html tags. If you or anybody reading this would give me a suggestion on how to do it, I will be happy to implement it in my example and, of course, credit the author as well. Thanks for your feedback Ady.

Mads Kristensen |

5/24/2006 4:53:46 AM #

Gavin

As .NET strings are immutable, the current solution will result in bad performance; the StringBuilder would be advisable.

Gavin |

5/24/2006 5:06:05 AM #

 Mads Kristensen

@ Gavin
I don't think a StringBuilder would bring any performance boost here. I concidered using it, but it is only efficient when doing many (more than 5) concatinations and that's not the case here. However, the method uses regex and the replace function, and maybe the StringBuilder handles them better than a string would. I simple test could give us the answer.

Mads Kristensen |

4/24/2008 4:40:10 AM #

pingback

Pingback from pimp.webdevelopernews.com

Removing Whitespace From Your Pages With ASP.NET

pimp.webdevelopernews.com |

7/17/2008 8:12:34 PM #

vira

if there is java script then it gives error

vira India |

10/17/2008 1:34:03 PM #

sajid

Can we use the below 3 techniques all at once to improve performance. Are there any side affects if I do so.

1. Enabling Gzip and Deflate HTTP Compression in ASP.NET pages (www.stardeveloper.com/.../display.html

2. Enabling HTTP Compression (IIS 6.0)
(www.microsoft.com/.../...85-bc4e-24eb4f312e0e.mspx

3. Remove whitespace from your ASP.NET page
(blog.madskristensen.dk/.../...ur-ASPNET-page.aspx)

sajid India |

2/10/2009 1:02:46 PM #

smith

fyi:  you've got an untrapped asp.net error on that safe-version link.


aside from that nasty 500 error showing its asp.net runtime bloomers Smile ... thanks for this.

smith United States |

3/5/2009 10:29:54 AM #

Michael Khalili

Much of the white space is caused by asp.net tabbing html tags. You can use the following two lines instead of the other replace statements to avoid some pitfalls.

html = Regex.Replace(html, "^\s+<", " <", RegexOptions.Multiline)
html = Regex.Replace(html, ">\s+<", "> <", RegexOptions.Multiline)

The first line removes all but one white space for tabbed html tags. It looks for only spaces and then < character at the start of a line.

The second one removes all but one white space between tags IF it ONLY has white space between it.

It doesn't remove all the bad white space but it gets a ton of it and lets you sleep at night knowing it doesn't mess with any other code.

I'm working on redesigning my website now and I'll probably use this in the design. Thanks for the idea!

Michael Khalili United States |

3/27/2010 4:20:38 PM #

trackback

Nutzlosen Whitespace zur Laufzeit aus dem HTML entfernen

Nutzlosen Whitespace zur Laufzeit aus dem HTML entfernen

klaus_b@.NET |

8/12/2010 12:12:36 AM #

pingback

Pingback from aio4s.com

Remove whitespace from your ASP.NET page |  All in one for social – Blog

aio4s.com |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008