About the author

Miron Abramson
Software Engineer,
CTO at PixeliT
and .NET addicted for long time.
Open source projects:
MbCompression - Compression library

Recent comments



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

© Copyright 2018

Creative Commons License

Blog Flux Directory
Technology Blogs - Blog Top Sites

Remove white spaces using HttpModule

Few weeks ago, Mads Kristens poseted a very usefull post about removing white spaces from your pages. For me, he found the best Regular Expression for this job.  He was using the Render method to do it, and it means that if you want to use it in existing site, you need to edit your base page and compile your site again.

I wanted to use it in some sites I have/manage, so I just insert his code into a new ResponseFilter and assign this filter in my Compression component, compile again this dll and replace it in all my sites. Importent thing is, that using this 'remove spaces metho' must folows the rule of that the current request is not made by MS-AJAX component/control, because the ViewState will be corrupted. This check can be easly made by this simple method:

internal static bool IsAjaxPostBackRequest(HttpContext context)
      return context.Request.Headers["X-MicrosoftAjax"] != null;

Also note, that RequestFilters are excuted in order LIFO (the last added filter is the first one to be performed) , so if you use any compression filter (like me), make sure you register the compression filter before this one.

The ResponseFilter code can be download from here:

RemoveWhiteSpaces.cs (2.34 kb)

The compression module with this filter included can be download from this post: Compression component.  In this component you can control in wich pages you want to anable or disable the remove spaces option. (exclude specified pages from compression and remove spaces)

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.NET | C# | Server side
Posted by Miron on Thursday, December 27, 2007 1:00 PM
Permalink | Comments (3) | Post RSSRSS comment feed

Related posts



Thursday, December 27, 2007 10:32 PM


Trackback from DotNetKicks.com

Remove white spaces from your pages easily using HttpModule

sulumits retsambew us

Sunday, May 31, 2009 9:26 PM

sulumits retsambew

this is nice tips.. thanks for sharing it

Seoul Man kr

Tuesday, May 25, 2010 10:44 AM

Seoul Man

Ah-ha. Perfect info as that answers a problem I spent several hours trying to work out. I did a search and ended up here. I'll have to check some of the other posts. Thank you!!!