Reputation: 7128
My pages are automatically being compressed by IIS7 with GZIP.
That is great... but, for one particular page, I need to stream it to the user, using Response.Flush()
when needed. But when the output is being compressed, the IIS server seems to collect all my output until the page is done before compressing and sending it to the client. That nullifies my attempt to Flush the content out to the user.
Is there a way that I can have this one page opt out of the compression?
I've determined that if I manually set the content type to one that does not match the IIS configuration at c:\windows\system32\inetsrv\config\applicationhost.config
, then IIS will not compress it. Eg. Response.ContentType = "x-text/html"
. This works okay with IE8, as it falls back to display the HTML. But Firefox will ask the user what to do with the unknown file type.
This could work, if there was another Mime Type I could use that browsers would accept as HTML, that is not matched in the applicationhost.config
. For reference, these are the mime types that will be compressed:
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
Are there other options to opt out of compression?
Upvotes: 13
Views: 2638
Reputation: 17724
You could use a custom made compression module, like this one:
HTTP compression of WebResource.axd and pages in ASP.NET
Using such it should be easy to customize which files to include/exclude.
Upvotes: 1
Reputation: 1
If you need a solution which depends only on C#, you may adapt this method I have written to cope with a problem in the Android Browser:
/// <summary>
/// Alters the current HTTP request only for Android user agents, in order to disable web page compression so the Android browser will not cut off most of the page content, based on the Content-length HTTP header.
/// </summary>
public static void fixAndroidPageDisplay()
{
HttpContext c = HttpContext.Current;
if (c == null) return;
HttpRequest r = c.Request;
if (r == null || r.UserAgent == null) return;
if (r.UserAgent.ToLowerInvariant().Contains("android"))
{
HttpResponse rsp = c.Response;
if (rsp != null)
{
string ce = null;
foreach (string s in rsp.Headers.Keys)
{
if (s != null)
{
if (s.ToLowerInvariant().Equals("content-encoding")) {
ce = s;
}
}
}
if (ce != null) {
rsp.Headers[ce] = "text/html";
rsp.Filter = rsp.OutputStream;
}
}
}
}
Upvotes: 0
Reputation: 1116
It may not be possible to disable compression for a certain page, but you can for a directory.
This describes how to disable static compression, but it may work for dynamic compression: (From http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/502ef631-3695-4616-b268-cbe7cf1351ce.mspx?mfr=true)
To disable static compression for only a single directory, first enable global static compression (if it is disabled) and then disable static compression at that directory. For example, to enable static compression for a directory at http://www.contoso.com/Home/StyleSheets, perform the following steps:
adsutil set w3svc/filters/compression/parameters/HcDoStaticCompression true
adsutil set w3svc/1/root/Home/StyleSheets/DoStaticCompression false
Upvotes: 3
Reputation: 10976
if you do Response.BufferOutput = false it will stop the inbuilt compression working, albeit not cleanly. You may get event warnings that it can't add headers after they have already been sent to the client.
Upvotes: 0
Reputation: 34587
Not sure I like this but maybe worth mentioning: Disable GZIP compression for IE6 clients
Upvotes: 1
Reputation: 10468
I know of no way of a page to disable itself programmatically during the request. However you can workaround the compression and send some extra padding garbage, enough for gzip to process a new block. Your padding data should be as random as possible so it doesn't get too compressed, filling the deflate buffer faster.
The actual amount of data to send depends on the compression module's configurations.
Upvotes: 0