Enable Web page compression in IIS6.0

Source: Internet
Author: User
1. What are the advantages of GZIP?
A: After Gzip is enabled, the data output to the user's browser will be compressed to reduce the amount of data transmitted over the network and increase the browsing speed.
2. How to enable the Gzip compression function of IIS:

A: First, if you need to compress static files (HTML), you need to create a directory on the hard disk and give it the write permission of the user "IUSR _ machine name. It is not required to compress dynamic files (PHP, asp, and aspx), because the pages are dynamically generated every time, so you can discard them after compression. In the IIS manager, right-click the website and choose "properties". The website is not the following site, but the whole website. Go to the "service" tab and choose to enable dynamic content compression and static content compression. Select the Server Extension under the website and create a new server extension. The name does not matter. The path of the file to be added below is: c: \ windows \ system32 \ inetsrv \ gzip. dll, and then enable this extension. Static content can be compressed at this time, but for dynamic content, the aspx file is not within the compression range. Because the default file that can be compressed does not have this extension. On the management interface, you cannot find a place where the extension can be added. At this time, you can only modify its configuration file. There is a MetaBase under c: \ windows \ system32 \ inetsrv. xml file. You can open it in notepad and find the IIsCompressionScheme. There are three segments with the same name, namely deflate, gzip, and Parameters. You do not need to worry about the third segment, the first two paragraphs have basically the same parameters. Add a line of aspx under the parameters HcScriptFileExtensions. If you have other dynamic programs to compress, add them here. Change HcDynamicCompressionLevel to 9 (0-10, 9 is the most cost-effective ). Then you need to restart the IIS service to realize the compression speed. (Note that to save the MetaBase. xml file, you must disable the iis admin service in the service)

The above steps only apply to html asp aspx php, which are effective for MVC and should be processed in the Code:

Write an ActionFilter to implement GZIP:

CompressFilterAttribute. cs

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Using System. IO;
Using System. IO. Compression;

Namespace MyMVC. ActionFilters
{
   Public classCompressFilterAttribute: ActionFilterAttribute
   {
       Public override void OnResultExecuting (ResultExecutingContextfilterContext)
       {
           String acceptEncoding = filterContext. HttpContext. Request. Headers ["Accept-Encoding"];
           If (String. IsNullOrEmpty (acceptEncoding) return;
           Var response = filterContext. HttpContext. Response;
           AcceptEncoding = acceptEncoding. ToUpperInvariant ();

           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 );
           }
       }
   }
}

 

In the controller File

       [CompressFilter]
       Public ActionResult Compress ()
       {
           Return this. View ();
       }

 

Site: http://blog.csdn.net/wdyllff/article/details/6652573
 

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.