IIS can perform compression on the fly and probably works much better than performing minification manually.

When people render their WebForms and MVC Views, they send their finished HTML back down to the page. The HTML contains all the markup required to run in the browser. Every developer knows all this though (and if not... well...).

Part of the whole grand ASP.NET pipeline is the Response.Filter. A seemingly innocent property with a lot of potential. Enough boasting about this guy, let's get onto the code and see what it can do.

First, you're going to need to create a custom stream. I'm hiding all the inherited members because they aren't going to mean much in this example, but don't forget that you need them.

With our custom stream, add a hidden property called the UnderlyingStream (honestly call it whatever you want). Lastly, accept a stream in the constructor and assign it to the underlying stream when you instantiate the new stream you're building.

Lastly, we're going to override .Write() routine to actually minify our output. A couple simple Regular Expressions and we can chop out most of the whitespace.

This is a very streamlined version of the code that doesn't take everything into account. You might want to put some special attention into preventing formatting of text in the pre tags or code tags, but it's a great start.

You could also assign this to a more global event that runs for all requests, but at that point you will need to write code to make sure you don't accidentally minify something like an image or a file (things that can't be compressed by just removing white-spaces).

You might wonder how much a little script like this would save. The answer is simple - It depends. Clearly, pages with a lot of white spaces will find a lot more gain. Sometimes it's 1 or 2 KBs (which will add up over the course of a month). Sometimes though, especially on larger pages, you can see some real savings.

Keep in mind that this code isn't bullet-proof, but instead it's here to get you started. You can continue to tweak the .Write() method to make sure that your code behaves the way you would like.