Compressing all pages through gzip or deflate will greatly reduce the page transfer capacity.
You can use fiddler2 to observe the page size changes after compression.
Add global. asax to the 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 {
10 // Gzip
11 app. response. Filter = new gzipstream (prevuncompressedstream, compressionmode. Compress );
12 app. response. appendheader ("content-encoding", "gzip ");
13}
14 else if (acceptencoding. Contains ("deflate "))
15 {
16 // defalte
17 app. response. Filter = new deflatestream (prevuncompressedstream, compressionmode. Compress );
18 app. response. appendheader ("content-encoding", "deflate ");
19}
20}
Fixed:
This method was accidentally found on the Internet. I did not use it in my actual project. The compression effect is yes, but I have not tested the resource usage on the server, so it is not clear that this is better than setting compression methods in IIS. For the moment, we only need to back up the code, and we will study it with no space in the future.
In addition, this method has some disadvantages. I found in the experiment that using it will cause small icons of the Treeview that comes with. Net to become invalid. The reason is that when Treeview uses the default small icon, the image file uses webresource. after the axd is obtained, the image cannot be obtained after compression. Therefore, when the Code starts, the requested file is filtered and webresource is retrieved. axd is not compressed.