The WebOptimizer class

by Mads Kristensen 8. February 2009 22:59

Last Wednesday I gave a presentation about performance optimization techniques in ASP.NET. In the demo I used a class called WebOptimizer that I’ve written specifically for that presentation, but based on techniques and code I’ve been using for years.

The static WebOptimizer class contains a few public helper methods that allow any web project to do HTTP compression, removal of whitespace from HTML, CSS and JavaScript, and support for conditional GET requests. After the presentation I promised to publish the class on my blog, so here it is. I hope you find it useful.

WebOptimizer.zip (2,15 kb)

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

Tags: ,

ASP.NET

Comments

2/9/2009 7:26:39 AM #

trackback

The WebOptimizer class

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout |

2/9/2009 10:07:10 AM #

awake


Good Stuff...

awake United States |

2/9/2009 12:11:40 PM #

mathmax

Great, but it would be nice to have a tool that do all these compressions automatically.
I mean, you just upload the css/html/js files on the server without compression. On each client request on these files, the compression will be automatically done and then stored in a temp file. The next time a file will be requested, the tool will redirect the request to the corresponding temp file. Of course the temp file would be regenerated if its date is older than the date of original file.

mathmax France |

2/9/2009 2:09:49 PM #

TweeZz

Mathmax... Why would you want to do that? To prevent that the compression happens on every request? Because if that's what you mean, then I'm pretty sure that Mads took care of this by caching the compressed version of each file on the server. Changing the original files would then invalidate the cached version causing the compression to happen again.
Am I correct? Smile

TweeZz Belgium |

2/9/2009 2:19:29 PM #

TweeZz

No TweeZz you are not correct Laughing
I had a look at the file now ;)

These are only some helper methods which can be used in HttpModules / HttpHandlers.
Mads has a bunch of them blog.madskristensen.dk/.../...nd-HttpHandlers.aspx

TweeZz Belgium |

2/9/2009 4:52:27 PM #

Frederik Schøning

Hi Mads,

thank you for the seminar - excellent stuff! And thank you for sharing the optimizer-class, I'm looking forward to implementing it on a larger site, I'm maintaining atm. I'm off hunting those 0 KB..

Hope to participate in the next geek-dinner.

Mvh. Frederik

Frederik Schøning Denmark |

2/9/2009 6:48:18 PM #

gk

@Mads: thanks again for sharing - all your small posts (and the greater ones too - ofcourse Smile ) really motivates to fire up VS now and then just to try some new idea your post triggered. Invaluable!

Now to the contents of this post - or rather the contents of the attachment. Wouldn't this line (#84):

body = body.Replace("\t", string.Empty);

in the method RemoveWhitespaceFromCss(...) possibly generate an error? What if a style is described as:

#someid p {.........}

where the whitespace between someid and p is a tab.

gk Norway |

2/9/2009 7:23:08 PM #

Søren Reinke

Hi Mads

Thanks a lot for the seminar.

Could you please include the code, that concatenated several css files into one ?

Søren Reinke Denmark |

2/9/2009 7:33:48 PM #

Mads Kristensen

@Søren,

You can get the CSS combiner code here blog.madskristensen.dk/.../...eets-at-runtime.aspx

@gk,

Yes, if that was a tab then the whitespace removal will corrupt your stylesheet. So don't do that Smile

Mads Kristensen United Kingdom |

2/9/2009 11:10:29 PM #

mathmax

TweeZz, as you say these are only helper method. I would like to have an automatic tool that made compressions when files are requested?

mathmax France |

2/10/2009 12:39:34 AM #

Mads Kristensen

@TweeZz,

Thanks for lending a hand. And BTW, by looking at the flag you've chosen, have you moved back to Belgium?

Mads Kristensen Denmark |

2/10/2009 1:36:08 AM #

Armin

Hey man thanks a lot
i hope sucessfull in life

Armin Iran |

2/10/2009 9:12:55 PM #

Søren Reinke

Hi Mads

Thanks for the link.

Would it be possible to get a copy of the project you showed at the seminar ?

I am having problems, getting whitespace stripping and compression to work at the same time.

Søren Reinke Denmark |

2/11/2009 3:44:06 AM #

TweeZz

@Mads

No I didn't move back to Belgium Smile Actually I never know which flag to choose. I definitely still feel more Belgian then Slovak, but I'm hardly ever there. Tough decision! Smile

@mathmax

In the link I posted there are several handlers / modules that do what you're asking for. Here are the ones you might want to have a look at.
- compress webresources: blog.madskristensen.dk/.../...ceaxd-in-ASPNET.aspx
- compress webresources and aspx pages: blog.madskristensen.dk/.../...pages-in-ASPNET.aspx
- remove white spaces in aspx pages: blog.madskristensen.dk/.../...e-for-ASPNET-20.aspx
- combine css files (reduces the amount or requests): blog.madskristensen.dk/.../...eets-at-runtime.aspx
- 'minify' css files: blog.madskristensen.dk/.../...3525-at-runtime.aspx
- minify css and js files: blog.madskristensen.dk/.../...avaScript-files.aspx

I'm using a combination of all those techniques in our asp.net projects.
Great stuff! Njummie..

TweeZz Slovakia |

2/16/2009 1:30:27 PM #

Prashant

Hi @Mads,

I tried your code for compressing page output using GZip. I have added your code on page load of my web page,

HttpContext context = HttpContext.Current;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip");
HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true;


But Its only compressing the .aspx file not the other external css & js files and images. Please tell me how can I compress my js, css and image files using code (becuase I am on shared hosting server and don't have access to IIS settings) in asp.net

Thanks,

Prashant India |

2/16/2009 3:59:59 PM #

Mads Kristensen

@Preshant,

You can find various ways of compressing other content on your website in this link collection blog.madskristensen.dk/.../...nd-HttpHandlers.aspx

Mads Kristensen Denmark |

5/7/2009 11:56:17 AM #

trackback

Realizzare un ActionResult per ottimizzare le nostre pagine web.

Realizzare un ActionResult per ottimizzare le nostre pagine web.

Il blog di ugo lattanzi |

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