How do I enable gzip compression when using MVC3 on IIS7?

54,537

Solution 1

You can configure compression through your web.config file as follows:

<system.webServer>
    <urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>

You can find documentation of this configuration element at iis.net/ConfigReference. This is the equivalent of:

  1. Opening Internet Information Services (IIS Manager)
  2. Navigating through the tree-view on the left until you reach the virtual directory you wish to modify
  3. Selecting the appropriate virtual directory so that the title of the right-hand pane becomes the name of said virtual directory.
  4. Choosing "Compression" under "IIS" in the right-hand pane
  5. Ticking both options and choosing "Apply" under "Actions" on the far right.

Note: (As pointed out in the comments) You need to ensure that Http Dynamic Compression is installed otherwise setting doDynamicCompression="true" will not have any effect. The quickest way to do this is:

  1. Start > Type optionalfeatures (this is the quickest way to get to the "Turn Windows Features on or off" window)
  2. Navigate to Internet Information Services > World Wide Web Services > Performance Features in the "Windows Features" treeview
  3. Ensure "Dynamic Content Compression" is ticked
  4. Click "Ok" and wait whilst Windows installs the component

Solution 2

You could do this in code if you rather do that. I would make a basecontroller which every control inherits from and decorate it with this attribute below.

public class CompressAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

        var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
        if (string.IsNullOrEmpty(encodingsAccepted)) return;

        encodingsAccepted = encodingsAccepted.ToLowerInvariant();
        var response = filterContext.HttpContext.Response;

        if (encodingsAccepted.Contains("deflate"))
        {
            response.AppendHeader("Content-encoding", "deflate");
            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
        }
        else if (encodingsAccepted.Contains("gzip"))
        {
            response.AppendHeader("Content-encoding", "gzip");
            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
        }
    }
}

Solution 3

Compression is enabled/disabled at the server's level. See IIS compression module in iis management console.

Here are the instructions for IIS from microsoft site.

Share:
54,537

Related videos on Youtube

imarkic84
Author by

imarkic84

Updated on July 14, 2020

Comments

  • imarkic84
    imarkic84 almost 4 years

    Does anybody know how to enable gzip compression in MVC 3? I'm using IIS7.

    Google Chrome Audit's result:

    1. Enable gzip compression (4)
    2. Compressing the following resources with gzip could reduce their transfer size by about two thirds (~92.23KB):
    3. /mydomain/ could save ~1.53KB
    4. jquery-1.4.4.min.js could save ~51.35KB
    5. Cufon.js could save ~11.89KB
    6. Futura.js could save ~27.46KB
  • Caleb Vear
    Caleb Vear about 12 years
    Its worth noting that you need to have the Http Compression Dynamic IIS option installed for this to work. The config reference link in this answer has the details.
  • irag10
    irag10 over 11 years
    On Windows Server 2008 optionalfeatures didn't work for me. Instead, navigate to Server Manager > Roles > Scroll to Web Server (IIS) > click Add Role Services. Now ensure that Web Server > Performance > Static Content Compression and Dynamic Content Compression are installed.
  • Paul Carroll
    Paul Carroll about 11 years
    @JustAnotherUserYouMayKnow - depends on how you are serving your resources; if you're pointing directly at files on the server then no, however if you serve them via actions (as our resource management system allows us) then it works brilliantly. BTW Rick Strahl has update this to support checking that the client actually supports gzip before compressing
  • Travis J
    Travis J almost 11 years
    Note that for redirecting it is important to protect against the filter being null. I used var response = ...;if( response == null || response.Filter == null)return;.
  • Travis J
    Travis J almost 11 years
    Also, you might want to use gzip first instead of deflate, read more here: stackoverflow.com/a/9856879/1026459
  • jjxtra
    jjxtra over 10 years
    Be aware that this can mess up your "Vary:" host header. More info: stackoverflow.com/questions/5372052/…
  • Chris Moschini
    Chris Moschini almost 10 years
    At least in my experience this compresses everything BUT the output of MVC Controllers. They for some reason return HTML without any gzip compression.
  • Akash Kava
    Akash Kava over 9 years
    Caution !!! IIS Dynamic Compression strips off Vary headers, its a bug or feature, I don't know, but implementing custom Filter makes more sense if you want cache to work correctly.
  • Muhammad Rehan Saeed
    Muhammad Rehan Saeed over 9 years
    IIS caches compressed files, so it will not have to compress them again for better performance. Use IIS if you can and only use this approach if IIS compression is not available to you.
  • aadi1295
    aadi1295 almost 9 years
    None of these solutions working on MVC5, IIS 8.5, .Net 4.5 Localhost. What should I do, any suggestions?
  • Jack Marchetti
    Jack Marchetti over 8 years
    YOu can set it in the web.config as well
  • Heemanshu Bhalla
    Heemanshu Bhalla about 8 years
    @Rob i am trying these steps but from point 1. Start > type i think there is some clierification missing in steps i could not find optionalfeatures and www services
  • Rob
    Rob about 8 years
    @HeemanshuBhalla, the comment from Rory above may apply to you, in Win2k8 server (and above?) "optionalfeatures" doesn't work/exist and you'll need to follow his steps
  • Sanjay Sahani
    Sanjay Sahani over 3 years
    @Rob I followed your steps and I get Content-Encoding as gzip but after refreshing page this header disappears
  • Rob
    Rob over 3 years
    @SanjaySahani, I'm afraid I don't know the answer to that; it may be worth asking a new question where you can go into detail