Click here to Skip to main content
14,634,605 members
Articles » Web Development » ASP.NET » General
Posted 24 Nov 2014


9 bookmarked

HTTP Respond Content Compression with MVC Controller Action Filter Attribute

Rate this:
5.00 (4 votes)
Please Sign up or sign in to vote.
5.00 (4 votes)
24 Nov 2014CPOL
HTTP respond data content length compression in MVC web application by an ActionFilter attribute to improve performance in web requests.


When we are using a web application, the application transmits large amount of data content between server and client via http responds.


We can reduce to some amount of data content http respond data content length using MVC controller action filter attributes. This helps a lot in web applications to improve performance.

Description with the Code

The following compress filter class inherited from ActionFilterAttribute class represents the "ActionFilter" for compressing any data content send server to client side in web application through http respond like AJAX in IIS server. This is a custom ActionFilter attribute class and you can write in filters folder in MVC web project.

using System.Web;
using System.Web.Mvc;

public class CompressFilter : ActionFilterAttribute
    public override void OnActionExecuting(FilterExecutingContext filterContext)
        HttpRequestBase request = filterContext.HttpContext.Request;

        string acceptEncoding = request.Headers["Accept-Encoding"];

        if (string.IsNullOrEmpty(acceptEncoding)) return;

        acceptEncoding = acceptEncoding.ToUpperInvariant();

        HttpResponseBase response = filterContext.HttpContext.Response;

        if (acceptEncoding.Contains("GZIP"))
            response.AppendHeader("Content-encoding", "gzip");
            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
        else if (acceptEncoding.Contains("DEFLATE"))
            response.AppendHeader("Content-encoding", "deflate");
            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);

Now, we can use compressFilter attribute bottom of any action in any controller class like the following code snippet.

public ActionResult Index()
 // code here

  return View(model);

And we can use filter attributes in any Ajax call action in controller class.

public ActionResult FilterRegions(string countryId)
        return Json(Model)

Run your application and analysis when your respond is compressed and not. You can feel how much data content is compressed with this method.

Note: When you compress the content whose content length is less than 1000, this method would not be successful, because that takes more time to compress than to send that amount of content.

You can see http content length through Firefox FireBug or Chrome Dev tool.

Normally GZIP compression is allowed by any browser today, even Internet Explorer.

Points of Interest

This method is very easy and successful to compress data content transmission via http request in web application.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Comments and Discussions

Questionmessy code Pin
Member 1151811611-Mar-15 19:19
MemberMember 1151811611-Mar-15 19:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.