Use gzip compression in ASP. NET

Source: Internet
Author: User

I often admire ASP. NET. Its overall architecture is actuallyElegant. Its component-based and scalable design enables us to focus on some aspects of our work. In this way, Business writers can focus on writing business code without having to worry about system permission design. System optimizers focus on system optimization instead of business logic. For some frequently used components, we can abstract them into user controls or custom controls. However, let's talk about it again. net only supports compiling code in page_load and button_click. It never understands how to compile code based on components and extensibility. Such people in the team will only blindly oppose the solutions designed by Excellent Architects, because they have no knowledge about ASP. NET except page_load and button_click.

Currently, mainstream Web browsers support gzip compression. Therefore, during the development of Web server programs, if a page generates a large HTML file, Gzip compression is performed before transmission, it can greatly reduce the output volume, accelerate the transmission efficiency, and greatly increase the user experience. Therefore, it is necessary to compress the web page output.

Adding the gzip compression function for ASP. net programs is actually very simple and does not need to modify any line of page logic code. You only need to add an httpmodule to process the relevant output. Of course, re-writing an iis api extension is also desirable. The open-source blog system blogengine. Net has a module dedicated to page compression. The design is very simple and practical. The main idea is to modify the httpresponse filter to compress the HTTP output stream. The main code of this module is as follows:

///// Add a callback to the prerequesthandlerexecute event during module initialization. /// Void ihttpmodule. INIT (httpapplication context) {If (blogsettings. instance. enablehttpcompression) {context. prerequesthandlerexecute + = new eventhandler (context_postreleaserequeststate );}}

The following describes the implementation of callback functions.Compressresponse (context );This line of code is enough.

////// Callback. /// Void context_postreleaserequeststate (Object sender, eventargs e) {httpcontext context = (httpapplication) sender). Context; If (context. currenthandler is system. Web. UI. Page
& Context. request ["http_x_microsoftajax"] = NULL & context. Request. httpmethod = "get") {// sets the compression of the output stream.Compressresponse (context );If (blogsettings. instance. compresswebresource) Context. response. Filter = new webresourcefilter (context. response. Filter);} else if (! Blogsettings. instance. compresswebresource & context. request. path. contains ("webresource. axd ") {context. response. cache. setexpires (datetime. now. adddays (30 ));}}

The following is the compressresponse implementation:

////// Compress the output. /// Public static void compressresponse (httpcontext context) {If (isencodingaccepted (deflate) {context. response. filter = new deflatestream (context. response. filter, compressionmode. compress); setencoding (deflate);} else if (isencodingaccepted (gzip) {context. response. filter = new gzipstream (context. response. filter, compressionmode. compress); setencoding (gzip );}}

The code is very simple, and the code line of the original system has not been modified, so it is truly scalable. This is also in line with the idea of AOP.

In the actual development process, many people like to write all the code to the Aspx. CS page, such as permission verification. In fact, I am very opposed to this, because this will make the entire system be like a bowl of soy sauce, and everything is simply joined together, which is difficult to maintain.

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.