Using HTTP compression in asp.net

Source: Internet
Author: User

Compressing all pages by gzip or deflate greatly reduces the volume of page transfers.

You can use Fiddler2 to observe changes in page size after compression.

Add Global.asax to your project, and add the following code:

1 protected void Application_BeginRequest (object sender, EventArgs e)
2 {
3 HttpApplication app = (HttpApplication) sender;
4 string acceptencoding = App. request.headers["Accept-encoding"];
5 Stream Prevuncompressedstream = App. Response.filter;
6 if (acceptencoding = null | | acceptencoding.length = 0) return;
7 acceptencoding = Acceptencoding.tolower ();
8 if (Acceptencoding.contains ("gzip"))
9 {
Ten//gzip
One app. Response.filter = new GZipStream (Prevuncompressedstream, compressionmode.compress);
App. Response.appendheader ("content-encoding", "gzip");
13}
Or else if (acceptencoding.contains ("deflate"))
15 {
//Defalte
App. Response.filter = new Deflatestream (Prevuncompressedstream, compressionmode.compress);
App. Response.appendheader ("content-encoding", "deflate");
19}
20}

Correction:

This method I accidentally found on the internet, I did not use in the actual project, compression effect is there, but on the server side of the resource consumption I have not tested, so it is not too clear this and IIS in the way to set the compression of that good. Temporarily just do a code backup, later with empty in the study.

In addition, there are drawbacks to this method, I found in the experiment that will cause the. NET from the TreeView's small icon failed. The reason is that the TreeView in the use of the default small icon, the picture file is obtained by WebResource.axd, and compression can not get the picture, so at the beginning of the code to filter the requested file, WebResource.axd do not compress.

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.