Introduction

Getting close to HTTP allows you to take advantage of the protocol. This means I can fully leverage features of HTTP such as caching, etags, status codes and the like. One of the important features of HTTP is Default Support for Compression.

Compression is a simple, effective way to save bandwidth and speed up your site. One of the major advantages of using HTTP protocol is default support of data compression using gzip/deflate including caching, etags. There is a lot of information available on this topic but no article explaining it with proof of concept. This article explains how to configure and verify HTTP Compression using Fiddler and trust me, it is fairly easy to do.

Determining Current request/response Encoding

In case you are using the localhost machine as a hosting environment for IIS and you don’t see HTTP request and response in fiddler, then we need to make an entry in web.config file to make Fiddler as a proxy for each HTTP request.

Ways to Apply Data Compression

  1. Using ASP.NET code
  2. Using IIS

Using ASP.NET Code in MVC

To implement this in ASP.NET MVC, we can utilize ActionFilterAttribute and override either OnActionExecuting or OnResultExecuting method.

The below code snippet is being used to check whether the current request browser can accept GZIP/DEFLATE encoding by looking at Accept-Encoding request header. If it finds GZIP encoding in this header, then we would set gzip in Content-encoding in response header and if it supports DEFLATE, then this code would set deflate in Content-encoding.

Apply this filter attribute to any action wherever you want to apply compression. In my case, I am applying it to About page of default MVC application.

Now, run the application from IIS and click on About menu from browser to access this page. Go back to the Fiddler and monitor Inspectors of this page request. You would find the compressed text and content-Encoding: gzip in raw text tab.

Using IIS

Before enabling data compression in IIS, we need to install Dynamic Compression module from the Server Manager Add Role and Feature.

Install Dynamic Compression Module

Below are few steps to install this module or you can follow this article that explains more in detail.

  1. Click StartAdministrative Tools | Server Manager.
  2. On the left-hand side, expand Rolesand then click on Web Server (IIS).
  3. Scroll down to the Role Servicessection and then click on Add Role Services. The Add Role Services wizard opens:
  4. On the SelectRole Services page, scroll down to the Performance section and select Dynamic Content Compression. Click on Next.
  5. Read the message and click Install.
  6. Once the installation is done, close the wizard.

Enable Compression in IIS

Levels

The procedures for configuring HTTP compression can be performed at the following levels in IIS:

  • Web Server
  • Site
  • Application
  • Physical and virtual directories
  • File (URL)

Steps

  1. Open IIS Manager and navigate to the level you want to manage. For information about opening IIS Manager, see Open IIS Manager (IIS 7). For information about navigating to locations in the UI, see Navigation in IIS Manager (IIS 7).
  2. In Features View, double-click Compression.
  3. On the Compressionpage, select the box next to Enable dynamic content compression.
  4. Click Applyin the Actions

Conclusion

Both approaches have their own advantages and disadvantages. Enabling Data Compression from IIS is very easy to configure and use but you don’t always have access to the IIS server to configure it, whereas enabling it from your ASP.NET code would give you a control on Action level data compression.

Compression of dynamic application responses can affect CPU resources because IIS does not cache compressed versions of dynamic output. If compression is enabled for dynamic responses and IIS receives a request for a file that contains dynamic content, the response that IIS sends is compressed every time it is requested. Because dynamic compression consumes significant CPU time and memory resources, use it only on servers that have slow network connections but that have CPU time to spare.

Unlike dynamic responses, compressed static responses can be cached without degrading CPU resources.