ASP. NET page Compression

Source: Internet
Author: User
Tags fiddler2

Introduction and application testing of several compression modules optimized by gzip for ASP. NET pages in real time! (Source Code)
Before the introduction, let's talk about ASP. the role and working principle of the gzip compression module on the Net Server. Many people use a large number of JS special effects or place a large number of dynamic advertisements when writing a webpage, usually some compression tools are used to locally compress pages or scripts and then upload them to the server. However, such compression tools generally have limited compression ratios and are not obviously optimized, the function of the compression module introduced in this article is to perform high-strength gzip compression on pages or scripts of Asp.net (generally, it can be compressed to only 1/5 of the size ), in addition, the compression process occurs when the client requests the ASPX page, which is compressed by the server and then transmitted to the client for display. In other words, after the project uses these compression modules, all aspx pages do not need to be compressed in advance. Instead, the compression module automatically compresses the page in the server background when the page is requested for the first time by the client (the compression process takes a short time, it is almost negligible, but the page loading speed after compression increases significantly ). The following describes several compression modules that implement such functions:

1. webresourcecompression compression module
This compression module is used to compress ASP in real time. all *. axd resources. Generally, when the ASPX page uses Ajax frameworks such as anthem.net or the Asp.net verification control, axd file references are generated. This file is actually a JS script. After this compression module is enabled, all axd resources are compressed by gzip and then sent to the client. This module is especially suitable for projects where the ASPX page applies the Ajax framework or the large axd resource file needs to be referenced!

Use: Decompress the package and place webresourcecompression. DLL to the bin directory of the website project. In the Web. config

<Add name = "webresourcecompression" type = "webresourcecompression. webresourcecompressionmodule"/>
Advantage: easy to use
Disadvantage: Only ASP. net2.0 or later versions are supported. Other resources except axd cannot be compressed!

Ii. pagecompression compression module
Unlike previous modules, this module is used to compress the ASPX page. After this compression module is enabled, the ASPX page will be compressed in real time, generally, KB of pages can be compressed to about 25 KB. This module is applicable to projects that only need to compress pages!

Use: Decompress compression. pagecompressionmodule. DLL to the bin directory of the website project, and add the following statement to the Web. config

<Add name = "pagecompressionmodule" type = "compression. pagecompressionmodule, compression. pagecompressionmodule"/>
Advantage: easy to use
Disadvantage: Only ASP. net2.0 or later versions are supported. Other resources except the ASPX page cannot be compressed. After compression is enabled, the axd resources referenced on the page will be invalid (Bug )!

Iii. httpcompress6.0 compression module
This is an ASP. net1.0/1.1/2.0 (versions earlier than 2.0 do not support gzip compression, only deflate compression), can compress all types of resources requested by the ASPX page (mimetypes, including images, JS scripts, axd, aspx pages, and CSS files. in config, you can customize the types of resources to be compressed, the types of resources not compressed, or the specified pages to be compressed or not compressed, there are also powerful custom functions such as setting the compression ratio (high | normal | low). However, the most serious problem with this control is in ASP. enabling compression in net2.0 will invalidate the axd file, so that if the Ajax framework is applied on the page, the Ajax function will become invalid.

Usage: omitted (similar to the compressionmodule compression module recommended below)
Advantages: open source code, powerful custom functions, support for ASP. net1.0/1.1/2.0, can compress a variety of resources!
Disadvantage: complicated use. Enabling compression under ASP. net2.0 will invalidate the axd file!

Iv. compressionmodule compression module (strongly recommended)
One module recommended by myself can also compress all resources requested by the ASPX page and compression modules that support powerful custom functions, like httpcompress6.0, you can customize the types of resources you want to compress and do not compress those types of resources. You can also define whether to compress the specified page or not to compress the specified page, in addition, the cache Resource function and the powerful function of the specified compressed directory are enabled. In addition, there will be no bug like httpcompress6.0 that the axd file is invalid, this compression module can perfectly support various Ajax frameworks !!

Usage: see the compressionmodule for actual use and performance testing!
Advantages: open source code, powerful custom functions, ability to compress a variety of resources, and perfect support for various Ajax frameworks!
Disadvantage: complicated use. Only ASP. net2.0 or later is supported!

To sum up, various compression modules can optimize pages. However, since the compression process occurs at one end of the server, enabling these compression will consume a little server resources, however, in general, the compression only occurs when the client accesses the page for the first time. Because the browser caches the page and resource relationships, the server resources will not be used for compression when the access is refreshed again, therefore, for developers who have a huge website page size, enabling the compression function is definitely worth some money. After all, the page size is smaller and loading will be faster!

Appendix: compressionmodule compression module instructions and performance tests!
I. Use
First, put DC. Web. httpcompress. DLL to the bin directory of the website project, and add and modify the Web. config of the Project as follows:

<Configsections>
<Sectiongroup name = "dcweb">
<Section name = "httpcompress" type = "DC. Web. httpcompress. configuration,
DC. Web. httpcompress "/>
</Sectiongroup>
</Configsections>

<Dcweb>
<Httpcompress compressiontype = "gzip">
<! -- Set whether to enable cachefiles and specify the cache directory. If this line of configuration is deleted, the default value is false (no cache is enabled). -->
<Cachesetaskcachefiles = "true" Path = "cache"/>
<! -- Set jspath and csspath. If this line of configuration is deleted, the default value will be used. -->
<Pathsettings jspath = "JavaScript" csspath = "CSS"/>
<! -- Configure the type of resources to be compressed. Here, only HTML is compressed, that is, only the ASPX page and *. axd resources will be compressed, and other resources (slice, CSS, etc.) will not be compressed! For example, if you want to compress a GIF image, add <add mime = "image/GIF"/> and <add mime = "image/JPEG"/> -->
<Shortdedmimetypes>
<Add mime = "text/html"/>
</Includedmimetypes>
<! -- Use the following to configure which types of resources are not compressed:
<Excludedmimetypes>
<Add mime = "text/html"/>
</Excludedmimetypes>
Note: Only one excludedmimetypes and includedmimetypes needs to be configured. If excludemimetypes is configured as above, other resources except HTML resources will be compressed -->
<Excludedpaths>
<! -- Set the page path for which compression is not enabled. The default. aspx page in the nocompress directory is set below. The compression function is not enabled for other pages. -->
<Add Path = "~ /Nocompress/default. aspx "/>
</Excludedpaths>
</Httpcompress>
</Dcweb>

<! -- The JS. axd and CSS. axd must be enabled to allow JavaScript and CSS
Compression -->
<Httphandlers>
<! -- If you do not need this function, you can delete this configuration section. -->
<Add verb = "*" Path = "Js. axd, CSS. axd"
Type = "DC. Web. httpcompress. compressionhandler, DC. Web. httpcompress"/>
</Httphandlers>
<! -- The compression module must be enabled for the webresource. axd to be
Compressed -->
<Httpmodules>
<Add name = "scriptmodule" type = "system. Web. Handlers. scriptmodule,
System. Web. Extensions, version = 1.0.61025.0, culture = neutral,
Publickeytoken = 31bf3856ad364e35 "/>
<! -- Set the parameters for enabling the compression module -->
<Add name = "httpcompressmodule"
Type = "DC. Web. httpcompress. httpmodule, DC. Web. httpcompress"/>
</Httpmodules>
After configuration, the compression module will work normally!

TIPS: If Web. config is configured
<Cachesetaskcachefiles = "true" Path = "cache"/>
The cache directory must exist in the project. Otherwise, an error will occur. If the project does not want to add this directory, you only need to delete this configuration section!

Ii. Performance Testing for practical applications
Tracking software: fiddler2
In this test, I developed the dotnettextbox online editor control as the page carrier. There are two identical pages: compress. aspx (enable compression) and nocompress. aspx (disable compression)

First, enable nocompress. aspx without compression and view the page volume as follows:

View fiddler2 and check the actually received traffic.

We can see that the size of the received and viewed properties is the same as 65452 bytes, and fiddler2 detects that the page is no compression.

Follow us to enable the compressed compress. aspx and view the page size (similar to the nocompress. ASPX page size, with a 2-byte error ):

Let's look at the actual traffic received by fiddler2.

The compression effect is finally seen. fiddler2 detects that the actual received traffic is only 17277 bytes, and the page in the lower right corner shows gzip encoding, the size of the compressed page is only 1/4 of the original size !!!

1) for the previous compressionmodule usage and performance test instances:
/Files/aspxcn/compresssample.rar

2) download the example of the compressionmodule compression module applied in ASP. NET Ajax:
/Files/aspxcn/ajaxsample.rar

3) complete source code of the compressionmodule compression module:
/Files/aspxcn/compressionmodule.rar

If you need other compression modules and source code mentioned above, you can visit your website, which contains all the compression modules and source code in this article for high-speed download:
Http://www.aspxcn.com.cn/default.aspx? Uid = 1, 108

From: http://www.itpub.net/thread-1068495-1-1.html

Related Article

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.