Use gzip and deflate protocols in httpmodule to compress the ASPX page

Source: Internet
Author: User
Currently, browsers generally support the gzip and deflate compression protocols. That is to say, when the server returns the compressed content using the gzip or deflate protocols, the browser automatically decompress the content. this can save a lot of network bandwidth, and the negative impact is to increase the burden on the server.

We only compress the ASPX page. Of course, we can also compress JS and CSS. however, if you want to compress the image, it will be wrong. The effect is the same as that of using WinZip to compress the image. You can only increase the size.

First, let's take a look at the pre-compression and post-compression page information of an instance ASPX page.
Before Compression

After compression

We can see that the effect of compressing to 27% of the original page size is still acceptable.
See detailsCodeCompressionmodule
# RegionUsing

UsingSystem;
UsingSystem. Web;
UsingSystem. Io. compression;

# Endregion

namespace blogengine. core. web. httpmodules
{< br> ///


/// compresses the output using standard gzip/deflate.
//
Public class compressionmodule: ihttpmodule
{

# RegionIhttpmodule members

///   <Summary>
/// Disposes of the resources (other than memory) used by the module
/// That implements <See CREF = "T: system. Web. ihttpmodule"> </See> .
///   </Summary>
Void Ihttpmodule. Dispose ()
{
// Nothing to dispose;
}

///   <Summary>
/// Initializes a module and prepares it to handle requests.
///   </Summary>
///   <Param name = "context"> An <See CREF = "T: system. Web. httpapplication"> </See>  
/// That provides access to the methods, properties, and events common
/// All application objects within an ASP. NET application.
///   </Param>
Void Ihttpmodule. INIT (httpapplication context)
{
If (Blogsettings. instance. enablehttpcompression)
Context. beginrequest + =   New Eventhandler (context_beginrequest );
}

# Endregion

# RegionCompression

Private   Const   String Gzip =   " Gzip " ;
Private   Const   String Deflate =   " Deflate " ;

///   <Summary>
/// Handles the beginrequest event of the context control.
///   </Summary>
///   <Param name = "sender"> The source of the event. </Param>
///   <Param name = "E"> The <See CREF = "system. eventargs"/> Instance containing the event data. </Param>
Void Context_beginrequest ( Object Sender, eventargs E)
{
Httpapplication app = Sender As Httpapplication;
// Compressing the ASPX page
If (App. Request. url. originalstring. toupperinvariant (). Contains ( " . Aspx " ))
{
// Compression protocol supported?
If (Isencodingaccepted (deflate ))
{
App. response. Filter =   New Deflatestream (App. response. filter, compressionmode. Compress );
Setencoding (deflate );
}
Else   If (Isencodingaccepted (gzip ))
{
App. response. Filter =   New Gzipstream (App. response. filter, compressionmode. Compress );
Setencoding (gzip );
}
}
}

///   <Summary>
/// Checks the request headers to see if the specified
/// Encoding is accepted by the client.
///   </Summary>
Private   Static   Bool Isencodingaccepted ( String Encoding)
{
Return Httpcontext. Current. Request. headers [ " Accept-Encoding " ] ! =   Null   && Httpcontext. Current. Request. headers [ " Accept-Encoding " ]. Contains (encoding );
}

///   <Summary>
/// Adds the specified encoding to the response headers.
///   </Summary>
///   <Param name = "encoding"> </param>
Private   Static   Void Setencoding ( String Encoding)
{
Httpcontext. Current. response. appendheader ( " Content-Encoding " , Encoding );
}

# Endregion

}
}

Code from blogengine.net created by jecray

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.