Things to learn: What is custom filter for http response compression in ASP.NET MVC application. how to write a custom filter in Asp.net C#, in this custom filter we will compress the HTTP response content and remove all spaces, this will help to reduce the page size, and will load faster
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 .
Let’s look at 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 needusing System; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Web.Mvc;
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); } } } }
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 }
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.