HTTP Compression gzip
HTTP Compression
The <httpCompression>
element specifies the HTTP compression settings for Internet Information Services (IIS) 7. HTTP compression can provide faster transmission times between IIS and client browsers that can accept compressed files.
An HTTP client must initiate communication for compressed content by sending the appropriate HTTP Accept-encoding header. If a client is not capable of HTTP compression, it will not pass that header and IIS 7 will always return uncompressed content.
There are two different types of compression that IIS 7 uses:
-
Static Compression:
IIS 7 caches compressed static content in the path that is specified by the directory attribute, which increases compression performance by eliminating the need to recompress content that has already been compressed. After IIS 7 has compressed a file, subsequent requests are given the compressed copy of the file from the cache directory.
The staticCompressionEnableCpuUsage and staticCompressionDisableCpuUsage attributes specify when IIS 7 will compress static files based on CPU usage.
You should use static compression with files that do not typically change, such as HTML files (*.html, *.htm), text files (*.txt), Microsoft Office documents (*.doc, *.xls, *.ppt), etc. The size of these files can be reduced through compression, which reduces download times for client requests and reduces bandwidth on the server.
Note
Image files such as *.jpg and *.png files are also static files, but typically they do not benefit from HTTP compression because these image files are already compressed.
-
Dynamic Compression:
Unlike static compression, IIS 7 performs dynamic compression each time a client requests the content, but the compressed version is not cached to disk. This change is made because of the primary difference between static and dynamic content. Static content does not change. However, dynamic content is typically content that is created by an application and therefore changes often, such as Active Server Pages (ASP) or ASP.NET content. Since dynamic content should change often, IIS 7 does not cache it.
The dynamicCompressionEnableCpuUsage and dynamicCompressionDisableCpuUsage attributes specify when IIS 7 will compress dynamic files based on CPU usage.
IIS 7 supports two different industry-standard compression schemes:
- Deflate - this form of compression is documented in Requests For Comment (RFC) specification 1951.
- GZIP (GNU zip) - this form of compression is documented in Requests For Comment (RFC) specification 1952.
Each of these two compression schemes can be enabled using the <schemes>
element of the <httpCompression>
element.
Note
While the <httpCompression>
element specifies the HTTP compression settings for Internet Information Services (IIS) 7, the <urlCompression> element specifies whether compression is enabled for a URL namespace.
Setup
HTTP compression is usually available on the default installation of IIS 7 and later. However, only static compression is installed by default. To install static or dynamic compression, use the following steps.
Windows 8 or Windows 8.1
- On the Start screen, move the pointer all the way to the lower left corner, right-click the Start button, and then click Control Panel.
- In Control Panel, click Programs and Features, and then click Turn Windows features on or off.
- Expand Internet Information Services, expand World Wide Web Services, expand Performance Features, and then select Dynamic Content Compression and/or Static Content Compression.
- Click OK.
- Click Close.
WebAPI Gzip when returning HttpResponseMessage
问题
服务器返回了gzip,但是客户端不识别。ajax和Fiddler都不识别
回答1
Add these NuGet packages:
Microsoft.AspNet.WebApi.Extensions.Compression.Server System.Net.Http.Extensions.Compression.Client
Then and add one line of code to App_Start\WebApiConfig.cs
:
GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));
That will do the trick!
Details at:
Hope that helps.
**Updated after comment from @JCisar
Update for ASP.Net Core
Nuget Package is
Microsoft.AspNetCore.ResponseCompression
https://benfoster.io/blog/aspnet-web-api-compression