document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //Gzip code tăng tốc độ load web asp.net
  2. Một số kỹ thuật giúp tăng tốc website bằng cách nén website trước khi đẩy về phía client thì Gzip compression giúp chúng ta làm được điều . Hôm nay mình sẽ hướng các bạn config Gzip để nén website nhé.
  3.  
  4. Trong project solution asp.net bạn tạo 1 file tên là global.asax và copy đoạn code sau
  5. trên mạng không có đoạn này mình bổ xung thêm mới chạy được
  6.  
  7. <%@ Application Language="C#" %>
  8. <%@ Import Namespace="System.IO" %>
  9. <%@ Import Namespace="System.IO.Compression" %>
  10.  
  11. <script runat="server">
  12. protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
  13.     {
  14.         HttpCompress((HttpApplication)sender);
  15.     }
  16.  
  17.  
  18.     private void HttpCompress(HttpApplication app)
  19.     {
  20.         string acceptEncoding = app.Request.Headers["Accept-Encoding"];
  21.         Stream prevUncompressedStream = app.Response.Filter;
  22.  
  23.  
  24.         if (!(app.Context.CurrentHandler is Page) ||
  25.             app.Request["HTTP_X_MICROSOFTAJAX"] != null)
  26.             return;
  27.  
  28.  
  29.         if (string.IsNullOrEmpty(acceptEncoding))
  30.             return;
  31.  
  32.  
  33.         acceptEncoding = acceptEncoding.ToLower();
  34.  
  35.  
  36.         if ((acceptEncoding.Contains("deflate") || acceptEncoding == "*")
  37.             && CompressScript(Request.ServerVariables["SCRIPT_NAME"]))
  38.         {
  39.             // deflate
  40.             app.Response.Filter = new DeflateStream(prevUncompressedStream,
  41.                 CompressionMode.Compress);
  42.             app.Response.AppendHeader("Content-Encoding", "deflate");
  43.         }
  44.         else if (acceptEncoding.Contains("gzip")
  45.             && CompressScript(Request.ServerVariables["SCRIPT_NAME"]))
  46.         {
  47.             // gzip
  48.             app.Response.Filter = new GZipStream(prevUncompressedStream,
  49.                 CompressionMode.Compress);
  50.             app.Response.AppendHeader("Content-Encoding", "gzip");
  51.         }
  52.     }
  53.  
  54.  
  55.     private static bool CompressScript(string scriptName)
  56.     {
  57.         if (scriptName.ToLower().Contains(".axd")) return false;
  58.         return true;
  59.     }
  60. </script>  
  61.  
  62. Ngoài ra bạn hãy vào file web.config cấu hình thêm như sau:
  63.  
  64. <system.webServer>
  65.     <httpCompression directory="%SystemDrive%\\inetpub\\temp\\IIS Temporary Compressed Files">
  66.  
  67.     <scheme name="gzip" dll="%Windir%\\system32\\inetsrv\\gzip.dll" />
  68.  
  69.     <dynamicTypes>
  70.  
  71.     <add mimeType="text/*" enabled="true" />
  72.  
  73.     <add mimeType="message/*" enabled="true" />
  74.  
  75.     <add mimeType="application/x-javascript" enabled="true" />
  76.  
  77.     <add mimeType="*/*" enabled="false" />
  78.  
  79.     </dynamicTypes>
  80.  
  81.     <staticTypes>
  82.  
  83.     <add mimeType="text/*" enabled="true" />
  84.  
  85.     <add mimeType="message/*" enabled="true" />
  86.  
  87.     <add mimeType="application/x-javascript" enabled="true" />
  88.  
  89.     <add mimeType="*/*" enabled="false" />
  90.  
  91.     </staticTypes>
  92.  
  93.     </httpCompression>
  94.  
  95.     <urlCompression  doStaticCompression="true" doDynamicCompression="false" />
  96.  
  97.     <staticContent>
  98.       <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00"/>
  99.          
  100.     </staticContent>
  101. </system.webServer>
');