Free Web Development Online Tutorials, Learn to Code
Enable HTTP Compression in ASP.NET MVC, Http Response compression in Asp.net C#

HTTP Compression in ASP.NET MVC

Response compression in ASP.NET

Why to compress your response content?
Compressing response content of your webpage means the size will get reduced and page will load much faster, compressed content is highly recommended for On page SEO This will improve performance of ASP.NET MVC web application significantly .

In this article you will learn how to compress the response content of any page using custom filter, in earlier article you have seen how to work with Filters in asp.net MVC

Here we show you how to write a custom filter that will be inherited from MyCompressFilter : System.Web.Mvc.ActionFilterAttribute

Here are the namespace reference you need
using System;
using System.IO;
using System.Text; 
using System.Text.RegularExpressions;
using System.Web.Mvc;

FilterAttribute class in C#

Here i have written a class called "MyCompressFilter", The class is inherited from MyCompressFilter : System.Web.Mvc.ActionFilterAttribute, this class will be used as Attribute on each Controller Class.

internal class MyCompressFilter : ActionFilterAttribute
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    { 
        var response = filterContext.HttpContext.Response;             
        if (filterContext.HttpContext.Request.RawUrl != "/sitemap.xml")
        {
            if (response.ContentType == "text/html" && response.Filter != null)
            {
            response.Filter = new HelperClass(response.Filter);
            }
        }
    }
}

http content compression using Stream

Now the below code is responsible for compression, it will remove all extra spaces from the all content is being rendered in browser. Now let's look at the implementation of HelperClass(response.Filter)

class HelperClass : Stream { private System.IO.Stream Base;
public HelperClass(System.IO.Stream ResponseStream)
{
if (ResponseStream == null)
throw new ArgumentNullException("ResponseStream");
this.Base = ResponseStream;
}
StringBuilder s = new StringBuilder();
public override void Write(byte[] buffer, int offset, int count)
{
string HTML = Encoding.UTF8.GetString(buffer, offset, count);
Regex reg = new Regex(@"(?<=\s)\s+(?![^<>]*</pre>)");
        HTML = reg.Replace(HTML, string.Empty);
        buffer = System.Text.Encoding.UTF8.GetBytes(HTML);
        this.Base.Write(buffer, 0, buffer.Length);
    }
                
#region Other Members
                
    public override int Read(byte[] buffer, int offset, int count) 
    { 
    throw new NotSupportedException(); 
    } 
                 
    public override bool CanRead { get { return false; } }                 
    public override bool CanSeek { get { return false; } }                 
    public override bool CanWrite { get { return true; } }
                 
    public override long Length { get { throw new NotSupportedException(); } }                 
    public override long Position
    { 
        get { throw new NotSupportedException(); } 
        set { throw new NotSupportedException(); } 
    }
                 
    public override void Flush()
    { 
        Base.Flush();
    } 
                 
    public override long Seek(long offset, SeekOrigin origin) 
    { 
        throw new NotSupportedException(); 
    } 
                 
    public override void SetLength(long value) 
    { 
        throw new NotSupportedException(); 
    } 
                 
#endregion 
}

Call Compression attribute on Controller

Now your custom filter class MyCompressFilter is ready, you can start using on all controller of your application, here is an example

[MyCompressFilter]
public class homeController : Controller
{
    public ActionResult index()
    {
        return View();
    }
}

After applying the compression attribute on any controller class, if you check the view source of any web page under that controller, you will notice that there is no extra spaces, and the view source size has been reduced thus performance improved.


Comment
Name
Email
Website
Subscribe
 
Http content compression