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