user626528
user626528

Reputation: 14417

Compression is not working

I need to compress all dynamic content of my data export site.
I've tried numerous ways, nothing works. Chrome shows that content is not compressed and "Content-Encoding" header is not present.

Trying to do it like this as the last resort method (before writing any response):

        context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
        context.Response.AppendHeader("Content-Encoding", "deflate");

Logging shows that this code is executed correctly. However, Chrome shows that content is not compressed, again.

UPD when using IIS built-in compression, it seems to work and request tracing shows "DYNAMIC_COMPRESSION_SUCCESS". However, IE still shows that response is not compressed. The same when I'm requesting the page from the server itself using localhost name.

Any ideas?

Upvotes: 4

Views: 1654

Answers (3)

John Culviner
John Culviner

Reputation: 22974

Instead of trying to do this manually I would rely on the pre-written (and tested) Microsoft code built into IIS that will do this for you:

Install Dynamic Content Compression on the machine (bullet 5 in the link) and enable it in IIS. IIS will now handle compression for on both static and dynamic content. Less code to maintain (and invariably have bugs) is always a good thing!

Upvotes: 4

Edi Wang
Edi Wang

Reputation: 3637

If you are using IIS7+, there's an Compression option. Navigate to your site, in the right main window, click "Compression", and check all 2 checkboxes:

  1. Enable dynamic content compression
  2. Enable static content compression

Upvotes: -1

ziyasal
ziyasal

Reputation: 558

If you want to do this manually first check the compression is supported,

public static bool IsGZipSupported()
{
    string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
    if (!string.IsNullOrEmpty(AcceptEncoding) &&
            (AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate")))
        return true;
    return false;
}

And compress your response,

public static void GZipEncodePage()
{
            if (IsGZipSupported()) {
                HttpResponse Response = HttpContext.Current.Response;

                string AcceptEncoding = HttpContext.Current.Request.Headers("Accept-Encoding");
                if (AcceptEncoding.Contains("gzip")) {
                    Response.Filter = new System.IO.Compression.GZipStream(Response.Filter, System.IO.Compression.CompressionMode.Compress);
                    Response.AppendHeader("Content-Encoding", "gzip");
                } else {
                    Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter, System.IO.Compression.CompressionMode.Compress);


            Response.AppendHeader("Content-Encoding", "deflate");
}

You can check filter is attached just before the headers are sent to the client

protected void Application_PreSendRequestHeaders()
{
    HttpResponse response = HttpContext.Current.Response;
    if (response.Filter is GZipStream && response.Headers["Content-encoding"] != "gzip")
        response.AppendHeader("Content-encoding", "gzip");
    else if (response.Filter is DeflateStream && response.Headers["Content-encoding"] != "deflate")
        response.AppendHeader("Content-encoding", "deflate");
}

For more information check this posts;

Upvotes: 4

Related Questions