Problems and Solutions after GZip compression is enabled in IIS

Source: Internet
Author: User
Document directory
  • Feedback
When the page file is large, enabling gzip compression can reduce the transmission traffic. The test results are very obvious. Some people in the garden use the gzip compression function of IIS, this function has also been applied in the project, and a difficult problem has been encountered during the use process. I have been searching for it for a long time and cannot solve it.

The excel2007 report file is generated on the server in the project. After the file is downloaded to the local computer, it cannot be opened normally, prompting that the file format is incorrect. Because the excel2007 file format is essentially a compressed package, I suspect that the file has been changed during the download process, but I do not know which part of the file has a problem. Here is a blog question I sent. I know how to help. Conjecture:1. because the excel2007 file is output during page render and end request is sent directly after output, we can infer that the compression mechanism of IIS should be added at the beginning of the request, instead of compressing at the end of the request. 2. When downloading an object, a download box is displayed, which may not be decompressed by the browser. This remains to be verified. Or another cause is that the compressed package cannot be pressurized because it has been packaged twice (once when the excel2007 file is generated, downloaded through IIS and packaged again ). Problems with IIS compression (currently found ):1. The configuration is inconvenient. To change the configuration file in the system, you cannot configure the file if you have insufficient permissions. 2. IIS6 affects all sites in IIS. It is said that iis7 does not have this problem. 3. After the IIS compression machine is configured, the configuration is lost several times after restart (strange ). 4. It is estimated that there will be problems with ooxml format when downloading excel2007. Solution:The solution is to use HttpModule for compression, use the PostReleaseRequestState event in HttpModule for compression, and delay the compression time after Render, generally, the code will not be uninstalled after the Render. :) since the PostReleaseRequestState event is after the Render event, some people may have doubts. You can also subscribe to it on the page, in my actual test, I found that the Response cannot be changed in the page logic. filter, an error will be reported. This may involve some permissions in asp.net, so we should implement them in HttpModule honestly. CompressModule
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Web;
Using System. IO;
Using System. IO. Compression;

Namespace CapabilityTest
{
Public class CompressModule: IHttpModule
{
# Region ihttpmodule members

Public void dispose ()
{
// Throw new notimplementedexception ();
}

Public void Init (httpapplication context)
{
Context. postreleaserequeststate + = new eventhandler (context_postreleaserequeststate );
}

# Endregion

Private const string GZIP = "gzip ";
Private const string DEFLATE = "deflate ";

Private void context_PostReleaseRequestState (object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
Stream filter = app. Response. Filter;

If (IsEncodingAccepted (app. Request, GZIP ))
{
App. Response. Filter = new GZipStream (filter, CompressionMode. Compress );
App. Response. AppendHeader ("Content-Encoding", GZIP );
}
Else if (IsEncodingAccepted (app. Request, DEFLATE ))
{
App. Response. Filter = new DeflateStream (filter, CompressionMode. Compress );
App. Response. AppendHeader ("Content-Encoding", DEFLATE );
}
}

Private static bool IsEncodingAccepted (HttpRequest request, string encoding)
{
String acceptEncoding = request. Headers ["Accept-Encoding"];
If (acceptEncoding = null)
Return false;

Acceptencoding = acceptencoding. tolower ();
If (encoding = gzip)
Return acceptencoding. Contains (gzip) | acceptencoding. Contains ("X-gzip") | acceptencoding. Contains ("*");

If (encoding = deflate)
Return acceptencoding. Contains (deflate );

Return false;
}
}
}
Improvement:1. You can add a custom ConfigurationSection to the config file to compress the specified file extension or exclude the compression of the specified file extension. 2. Some people on the network say they want to deal with Microsoft's AJAX and cannot compress it. The method to judge is return (app. Request ["HTTP_X_MICROSOFTAJAX"]! = Null | app. Request ["Anthem_CallBack"]! = Null); 3. it can be compressed or not compressed for pages that implement a base class or interface: ICompressable p = app. context. handler as ICompressable; return (p = null );In addition:This article is intended for IIS6. The latest iis7 has not been personally experienced, and I do not know whether compression is doing well. The postreleaserequeststate event mentioned in this article is only supported in Asp.net 2.x, so it cannot be used in projects developed by vs2003. Complete code download tag tags: gzip, IIS compression, httpmodule0 0

0

(Please comment on the article)

Feedback #1 floor reply reference View

By Goodspeed or iis7 is relatively good, Just configure it. # On the second floor, [the landlord] Reply to the reference for viewing

By ppchen (Chen ronglin) @ Goodspeed
I have never used iis7. Find a time to try it #85.19.68. * reply to reference on the third floor

By Liu Tao [unregistered user] Thank you for sharing. It is useful #219.232.59. * reply to reference

By jisen007 [unregistered users] It feels good, but I found a problem during the test. I can only compress aspx, JS and CSS files, and I tracked it, the compressed code is executed, but it has no compression effect. Why? # On the fifth floor, [the landlord] Reply to the reference for viewing

By ppchen (Chen ronglin) @ jisen007
I configured web. comfig, maybe it was not configured.

From: http://www.cnblogs.com/ppchen/archive/2009/02/19/1382530.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.