Click here to Skip to main content
11,502,547 members (64,811 online)
Click here to Skip to main content

Action Filter for Compression

, 17 Apr 2014 CPOL 6.1K 6 10
Rate this:
Please Sign up or sign in to vote.
This tip will help to create an action filter to compress the contents like Json, partial view, etc.


This tip will help to create an action filter to compress the contents like Json, partial view, etc.

Using the Code

Step 1: Create a class which will be derived from ActionFilterAttribute.

Step 2: Overwrite the method OnActionExecuting.

 public class CompressAttribute : ActionFilterAttribute
        public override void OnActionExecuting(ActionExecutingContext filterContext)
            HttpRequestBase request = filterContext.HttpContext.Request;
            string Encodingtype = request.Headers["Accept-Encoding"];
            if (string.IsNullOrEmpty(Encodingtype)) return;
            Encodingtype = Encodingtype.ToUpperInvariant();
            HttpResponseBase responseMessage = filterContext.HttpContext.Response;
            if (Encodingtype.Contains("GZIP"))
                responseMessage.AppendHeader("Content-encoding", "gzip");
                responseMessage.Filter = new GZipStream(responseMessage.Filter, CompressionMode.Compress);
            else if (Encodingtype.Contains("DEFLATE"))
                responseMessage.AppendHeader("Content-encoding", "deflate");
                responseMessage.Filter = new DeflateStream(responseMessage.Filter, CompressionMode.Compress);

Step 3: In this method, check the request header if it contains the gzip or deflate encoding. If yes, then add the response with the compression filter.

Step 4: Use the attribute on top of action of a controller.

        public ActionResult ShowMeritUsageResults(FormCollection model)
  // test code....

Points of Interest

Install the fiddler, and see the difference between the size before and after compression. Please find the code here.


  • 17th April, 2014: Initial version


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


About the Author

gunjan k saxena
Team Leader Wipro
United States United States
No Biography provided
Follow on   LinkedIn

Comments and Discussions

GeneralMy vote of 5 Pin
Volynsky Alex20-Apr-14 6:09
professionalVolynsky Alex20-Apr-14 6:09 
GeneralRe: My vote of 5 Pin
gunjan k saxena20-Apr-14 7:35
membergunjan k saxena20-Apr-14 7:35 
GeneralRe: My vote of 5 Pin
Volynsky Alex20-Apr-14 7:59
professionalVolynsky Alex20-Apr-14 7:59 
Questionsample project Pin
fredatcodeproject17-Apr-14 10:43
memberfredatcodeproject17-Apr-14 10:43 
AnswerRe: sample project Pin
gunjan k saxena17-Apr-14 10:49
membergunjan k saxena17-Apr-14 10:49 
GeneralRe: sample project Pin
fredatcodeproject17-Apr-14 10:56
memberfredatcodeproject17-Apr-14 10:56 
GeneralRe: sample project Pin
gunjan k saxena20-Apr-14 8:20
membergunjan k saxena20-Apr-14 8:20 
GeneralRe: sample project Pin
fredatcodeproject20-Apr-14 8:51
memberfredatcodeproject20-Apr-14 8:51 
GeneralRe: sample project Pin
gunjan k saxena21-Apr-14 3:41
membergunjan k saxena21-Apr-14 3:41 
GeneralRe: sample project Pin
fredatcodeproject23-Apr-14 11:01
memberfredatcodeproject23-Apr-14 11:01 
GeneralMessage Removed Pin
fredatcodeproject29-Apr-14 10:33
memberfredatcodeproject29-Apr-14 10:33 
GeneralRe: sample project Pin
gunjan k saxena29-Apr-14 10:36
membergunjan k saxena29-Apr-14 10:36 
GeneralRe: sample project Pin
fredatcodeproject29-Apr-14 10:59
memberfredatcodeproject29-Apr-14 10:59 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 17 Apr 2014
Article Copyright 2014 by gunjan k saxena
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid