Minification of HTML in ASP.NET

Minification refers to the process of removing unnecessary or redundant data without affecting how the resource is processed by the browser – e.g. code comments and formatting, removing unused code, using shorter variable and function names, and so on..

In today’s post, we are gonna quickly learn how to minify HTML for Asp.Net. We will Minify the HTML produced by ASP.NET by using an 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 HtmlMinification
{
    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 HtmlMinification
{
    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);
                }
        }
 }
}

Then configure your web.config

<system.webserver>
  <modules>
    <add name="HtmlMinify" type="HtmlMinification.HttpModule, MinifyHTML"></add>
  </modules>
</system.webserver>

Here is how you can quicly minify the HTML.

Please feel free to comment to this post or you can drop me an email at naik899@gmail.com

The post Minification of HTML in ASP.NET appeared first on Ravindra Naik.

Comments

Popular posts from this blog

Serve GZip CSS and JS from AWS S3

Create a Central Logging System

Create a calendar invite (ics file) in ASP NET