Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7059021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:10:56+00:00 2026-05-28T04:10:56+00:00

Using ASP.Net 2.0 and IIS 6 I enabled compression by extending IHttpModule and registering

  • 0

Using ASP.Net 2.0 and IIS 6

I enabled compression by extending IHttpModule and registering in the Web.config file

public class EnableCompression : IHttpModule
{

    public void Init(HttpApplication application)
    {
        application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
    }

    private void Application_BeginRequest(Object source, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        String encoding = context.Request.Headers.Get("Accept-Encoding");

        if (encoding == null)
            return;

        encoding.ToLower();

        if (encoding.Contains("gzip"))
        {
            context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
            HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
        }
        else
        {
            context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
            HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate");
        }
    }

    void IHttpModule.Dispose()
    {
        throw new Exception("The Method or Operation is not Implemented");
    }

}

The above works just fine until I also set an expires header in the page load event on my master page code file

protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
    Response.Cache.SetValidUntilExpires(true);
    Response.Cache.SetLastModified(DateTime.Now);
} 

After setting the expires header, I get a bunch of garbage once the browser retrieves data from cache. So the first time a page is loaded, it’s fine. If I go to a new page and then got back to the first page, I only get a page full of:

��`I�%&/m�{J�J��t��`$ؐ@�����iG#)�*�

If I disable compression but leave the expires header, it’s fine
If I disable the expires header but leave compression enabled, it’s fine
If I enable both, I get a page full of garbage.

I have no idea what’s going on.

Response Header
HTTP/1.1 200 OK
Date: Sun, 08 Jan 2012 07:23:15 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Content-Encoding: gzip
Cache-Control: private
Expires: Sun, 08 Jan 2012 07:33:00 GMT
Last-Modified: Sun, 08 Jan 2012 07:23:00 GMT
Vary: *
Content-Type: text/html; charset=utf-8
Content-Length: 13613

Request Header
GET /eng/DE/ HTTP/1.1
Host: wwwnpg
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cookie: ASP.NET_SessionId=gxdxwnnum5rumue13rxmc5mb
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:10:57+00:00Added an answer on May 28, 2026 at 4:10 am

    You should check your HTTP headers with a web proxy like Fiddler to be sure, but I suspect what’s happening is that the runtime is caching your pre-filtered content, as well as the headers you set. So the browser ends up thinking that the content is compressed, when it actually isn’t.

    The solution is to set your compression Filter later in the page lifecycle, such as in the PostRequestHandlerExecute event in your HttpModule. I use that event to set a whitespace Filter, and it works fine for me with the output cache (with IIS7+).

    Another reason to set the Filter late in the lifecycle is to handle HTTP error pages. Otherwise, if your page throws an Exception, the Filter might be set, but the runtime will discard any custom headers.

    You should also vary the output cache based on the Accept-Encoding header, by setting VaryByHeader in the OutputCache directive:

    <%@ OutputCache Duration="60" VaryByParam="None"
        VaryByHeader="Accept-Encoding" %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ASP.NET MVC to develop a web application, deploying to IIS 7. I've
iam using c#,asp.net and iis, i want to simulate slow internet connection on my
We are using IIS 6 and ASP.Net, When users make secure page requests using
Applications I am using: Visual Studio 2008 (C#/ASP.NET) Visual Source Safe 8.0 IIS 5.1
I have a ASP.NET website deployed under IIS uisng virtual directories. I'm using forms
Using ASP.net, how do you write ANSI characters to a text file? I need
I am using ASP.Net + .Net 3.5 + VSTS 2008 + IIS 7.0 +
I currently have an ASP.NET 3.5 SP1 running on IIS 7. I have enabled
Summary: I have an ASP.NET MVC website in IIS named 'Website' using an AppPool
I'm publishing a public ASP.NET website. I'm assuming the visitor is using Windows and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.