ASP. NET MVC 3 website Optimization Summary (i) using Gzip compression

Source: Internet
Author: User
Tags httpcontext

The benefits of Gzip compression on the Web site believe that many people are already aware that doing so can improve the performance of the site. So why do many websites not have Gzip compression enabled? There are 4 reasons: antivirus software, browser bugs, site agents, and servers are not configured.

The accept-encoding request header is not sent when using IE6, so the Gzip compression feature is not supported, so call us to use Google Chrome. In ASP. 3, we implemented Actionfilter by implementing the following:

Public Class Compressattribute : ActionFilterAttribute{ Public Override void OnActionExecuting(ActionExecutingContextFiltercontext) { VarAcceptencoding=Filtercontext.HttpContext.Request.Headers["Accept-encoding"]; If (!String.IsNullOrEmpty(Acceptencoding)) {Acceptencoding=Acceptencoding.ToLower(); VarResponse=Filtercontext.HttpContext.Response; If (Acceptencoding.Contains("Gzip")) {Response.Appendheader("Content-encoding", "Gzip");Response.Filter = New GZipStream(Response.Filter, Compressionmode.Compress); } Else If (Acceptencoding.Contains("Deflate")) {Response.appendheader ( "content-encoding" ,  "deflate"  Response.filter = new  deflatestream (response filter, compressionmode compress);  } } }< Span class= "PLN" >}            

This allows us to add [Compress] on the Action to implement gzip compression to achieve gzip compression, as follows:

[Compress]publicactionresultIndex(string ID)     

Here we've implemented gzip compression in ASP. NET MVC 3, and we can use Google Chrome to see if gzip compression is properly implemented. Use the shortcut key ctrl+shift+j to open the developer tools and view the results, such as finding that Gzip compression has been implemented:

This is the first article of the ASP. NET MVC 3 site optimization, and there are several articles behind me that I will be sorting out, hoping to help improve the performance of ASP. NET MVC 3.

ASP. NET MVC 3 website Optimization Summary (i) using Gzip compression

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.