Manually Compressing Any ASP.NET Page, PageMethod, MVC Action, HTTPHandler,..

Compressing A Single ASP.NET Response Manually

This post is about compressing your HTTP result without using IIS Dynamic Compression. I’ll save the story to the end and give you the code first:

Well, this shows how to do the compression itself. Depending on how you do ASP.NET, you probably will call it differently.

In my case, I called it manually from an ASP.NET Webforms PageMethod (more on why below), but if you are using ASP.NET MVC for example, you probably want to wrap it in an ActionFilter and apply that to the action you want to compress its output. Let me know in the comments or on twitter if you have a problem implementing it in a particular situation.

IIS Dynamic Compression

IIS 7+ has built in dynamic compression support (compressing output of server-side scripts like ASP.NET, PHP, etc.). It’s not by default because compressing dynamic content means running the compression for every request (because it doesn’t know what the server-side script will generate for every request, the point of using server-side programming is generating dynamic content!).

Static compression on the other side (caching static files like styles and scripts) is on by default because once the static resource is compressed, the compressed version is cached and served for every future request of the same file (unless the file changes of course).

General advice: I’d say if your server side scripts expect to return large text-based content (say, large data, even after paging, etc. like large reports or whatever), always turn dynamic compression on, at least for the pages that expect to return large data sets of text.

In many cases though the majority of large files will be scripts (and possibly images) will be the larger parts though, which are often taken care of (for scripts for example) by IIS static compression or ASP.NET Bundling.

Share With Friends:

P.S. Please help me out by checking this offer, then look below for a small Thank You.

How did I learn that?

As a bonus for coming here, I'm giving away a free newsletter for web developers that you can sign up for from here.
It's not an anything-and-everything link list. It's thoughtfully collected picks of articles and tools, that focus on Angular 2, ASP.NET 5, and other fullstack developer goodies.

  • Shiras

    How do I get the compressed data. so that I can send on my own next time instead of compressing each and every time.

    please advice.

    • Instead of using the response stream, you can use another stream in memory or file or whatever. However, if the data changes, you want to compress every time, if the data doesn’t change for some conditions or some users or so, then you may use caching, and avoid the entire processing.

      • Shiras

        Thanks for your valuable response.

        What I am trying to do is.
        Get the compressed data for the first time.
        I want to save this compressed data to database.

        I tried using Memory stream.
        I was not able to save the byte array to File Stream. Because when I save into a .txt file. i see the same string instead of byte array.

        can you please help with a sample code snippet me in getting the compressed data to variable.

        Lot of people are having the same question.

        • Shiras

          @Meligy can you please update.

        • Hi Shiras,
          Sorry for not replying earlier. What you are after is a generic use of GZipStream. You can find plenty of examples in where you just replace the “new GZipStream(…)” with what I used here.

          I haven’t done it myself though, and don’t think it’s a good idea (need to measure whether the extra overhead adds costs instead of eliminating them).