Remove White Space in HTML Output

by Michael Khalili on September 28, 2009

If you’ve looked at the HTML source of an ASP.NET page you know there’s a ton of extra white space that isn’t necessary. If you’re concerned about page load speed and bandwidth you’ll want to remove all that extra space. Here’s some code you can use to reduce page size by an average of over 10%. I put this in my MasterPage file.

Protected Overloads Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    Dim swHtmlText As New HtmlTextWriter(New System.IO.StringWriter)

    MyBase.Render(swHtmlText)

    Dim strHtml As String = swHtmlText.InnerWriter.ToString

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

    writer.Write(strHtml.Trim)

End Sub

The replacements I make are very conservative. You can get even more extreme by replacing all spaces after > and before < with the regex SingleLine option.

strHtml = Regex.Replace(strHtml, "\s+<", " <", RegexOptions.Singleline)
strHtml = Regex.Replace(strHtml, ">\s+", "> ", RegexOptions.Singleline)

That code could will break text in a Textfield so you have to make sure to test this more extreme version. You can also add a property in your code for the level of white space you want removed.

You should share this page:
  • email
  • HackerNews
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Mixx
  • Sphinn
  • Yahoo! Buzz
  • Print
  • michele
    Hello, great code, but unfortunately it's not compatible with .net ajax... if controls are inside an updatepanel you receive an error... :(
    do you have any solutions?
    Thanks
  • I don't use update panels in .net. I use jQuery and JavaScript calls instead but maybe I can still help. What's the error you're getting?
  • Michele
    Hi, Thanks for the reply. The error I get is:

    Errore: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
    Details: Error parsing near 'ol">Iva</th><th scope="col">Totale</th><'.
    File sorgente: http://localhost/Memento/ScriptResource.axd?d=m...
    Riga: 5

    if I just execute writer.write(strHtml) without any replace it works (but of course the page code is still with whitespaces)
  • Could this possibly be a fix?
    http://weblogs.asp.net/albertpascual/archive/20...

    If not, what version of Ajax & .net are you using?
  • Michele
    eehhh... I tried to disable eventvalidation, but with no results... I am using VS2008, .Net Framework 3.5 and Ajax 3.5.
    Actually, I have this error only on few pages, so I created a new class for my pages and I am inheriting the new class only in the pages that do not have this error...
  • I think you might be altering script in addition to html. Try using fiddler or firebug to see the response you're getting back from the server to make sure it's what you're expecting.

    At least you're able to create a workaround for the broken pages. It might be a minor enough issue that it's not worth digging deeper. The real killers are full page loads more than ajax updates.
  • Michele
    mmhhh... I was looking with firebug, the scripts are not altered... the problem looks like in certain cases ajax needs the "original version" of the html that the page rendered, and only 1 change (like adding some text something to the response) causes the failture to the postback...
    anyway, I already solved 80% of the performance issue I had on this site, so I am pretty happy...
    Thanks a lot for your code and your answers and have a nice day!!
    Michele
blog comments powered by Disqus

Previous post: Textbox and Dropdown combined

Next post: Use Bit.ly to Expose Click Traffic of Tweets