Minify HTML for ASP.NET

In this example we will Minify the HTML produced by ASP.NET by using a HttpModule to remove all white spaces from the Response Stream.

First lets create the Filter for removing the white spaces


using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace MinifyHTML
{
    internal class WhitespaceFilter : Stream
    {
        private static readonly Regex Pattern = new Regex(@"^\s+", RegexOptions.Multiline | RegexOptions.Compiled);

        private readonly Stream _stream;

        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        public override long Length
        {
            get { return 0; }
        }

        public override long Position { get; set; }

        public override void Flush()
        {
            _stream.Flush();
        }


        public override int Read(byte[] buffer, int offset, int count)
        {
            return _stream.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _stream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            _stream.SetLength(value);
        }

        public override void Close()
        {
            _stream.Close();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            byte[] data = new byte[count];

            Buffer.BlockCopy(buffer, offset, data, 0, count);

            string content = Encoding.Default.GetString(buffer);

            content = Pattern.Replace(content, string.Empty);

            byte[] output = Encoding.Default.GetBytes(content);

            _stream.Write(output, 0, output.GetLength(0));
        }

        public WhitespaceFilter(Stream stream)
        {
            _stream = stream;
        }
    }
}

Now lets create the HttpModule


using System;
using System.Web;

namespace MinifyHTML
{
    public class HttpModule : IHttpModule
    {
        void IHttpModule.Init(HttpApplication context)
        {
            context.BeginRequest += ContextBeginRequest;
        }

        private void ContextBeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;

            if (app != null)
                if (app.Request.RawUrl.Contains(".aspx"))
                {
                    app.Response.Filter = new WhitespaceFilter(app.Response.Filter);
                }
        }

        void IHttpModule.Dispose()
        {
            // Nothing to dispose; 
        }
    }
}

Then configure your web.config

Using IIS 7


  
    
  

Using IIS 6


  
    
  

You can download the Minify HTML for ASP.NET project here



Comments

  1. frank March 4th

    Comment Arrow

    Why not extend this to also remove HTML comments?
    HTML comments take more bytes than whitespace alone.
    If you do add this functionality be aware of IE conditional CSS comments.


  2. Sachin Sangekar March 3rd

    Comment Arrow

    css and js are not loading correctly after using this solution.


Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.