Mở/Bật Nén Gzip cho website Asp.net thành công 100%

Thảo luận trong 'Lập trình web Asp.net' bắt đầu bởi admin, 25/11/16.

  1. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,897
    Đã được thích:
    1,198
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Hôm nay mình hướng dẫn các bạn code nén gzip cho code asp.net file css, js thành công 100% mà mình đã sử dụng được.

    Trước tiên bạn tạo file Global.asax sau đó click chuột phải vào file chọn như hình dưới.

    nen-gzip-cho-web-asp.net.jpg

    Sau đó bạn chèn đoạn code sau vào file Global.asax này
    Mã:
    <%@ Application CodeBehind="Global.asax.cs" Inherits=".........Global" Language="C#" %>
    Mã:
    <%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="System.IO.Compression" %>
    <script RunAt="server">
        protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpCompress((HttpApplication)sender);
        }
        private void HttpCompress(HttpApplication app)
        {
            string acceptEncoding = app.Request.Headers["Accept-Encoding"];
            Stream prevUncompressedStream = app.Response.Filter;
            if (!(app.Context.CurrentHandler is Page) ||
                app.Request["HTTP_X_MICROSOFTAJAX"] != null)
                return;
            if (string.IsNullOrEmpty(acceptEncoding))
                return;
            acceptEncoding = acceptEncoding.ToLower();
            if ((acceptEncoding.Contains("deflate") || acceptEncoding == "*")
                && CompressScript(Request.ServerVariables["SCRIPT_NAME"]))
            {
                // deflate
                app.Response.Filter = new DeflateStream(prevUncompressedStream,
                    CompressionMode.Compress);
                app.Response.AppendHeader("Content-Encoding", "deflate");
            }
            else if (acceptEncoding.Contains("gzip")
                && CompressScript(Request.ServerVariables["SCRIPT_NAME"]))
            {
                // gzip
                app.Response.Filter = new GZipStream(prevUncompressedStream,
                    CompressionMode.Compress);
                app.Response.AppendHeader("Content-Encoding", "gzip");
            }
        }
        private static bool CompressScript(string scriptName)
        {
            if (scriptName.ToLower().Contains(".axd")) return false;
            return true;
        }
    </script>
    
    Sau đó vào web.config tìm tới node <system.Webserver>…</system.Webserver> chèn vào bên trong đoạn code sau:
    Mã:
        <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
          <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
          <dynamicTypes>
            <add mimeType="text/*" enabled="true" />
            <add mimeType="message/*" enabled="true" />
            <add mimeType="application/x-javascript" enabled="true" />
            <add mimeType="*/*" enabled="false" />
          </dynamicTypes>
          <staticTypes>
            <add mimeType="text/*" enabled="true" />
            <add mimeType="message/*" enabled="true" />
            <add mimeType="application/x-javascript" enabled="true" />
            <add mimeType="*/*" enabled="false" />
          </staticTypes>
        </httpCompression>
        <urlCompression  doStaticCompression="true" doDynamicCompression="false" />
        <staticContent>
          <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00"/>
        </staticContent>
    
    Tiếp theo vào developers.google.com/speed/pagespeed/insights để kiểm tra hoặc checkgzipcompression.com
     
    Cảm ơn đã xem bài:

    Mở/Bật Nén Gzip cho website Asp.net thành công 100%

    phuongxoan thích bài này.
  2. phuongxoan
    Tham gia ngày:
    7/4/15
    Bài viết:
    173
    Đã được thích:
    42
    Điểm thành tích:
    28
    admin thích bài này.
  3. kiwi lạnh
    Tham gia ngày:
    15/11/16
    Bài viết:
    4
    Đã được thích:
    0
    Điểm thành tích:
    1
    Giới tính:
    Nữ
    cảm ơn ad bài viết rất hay. mong ad đăng lên nhiều bài hay hơn nữa
     


Chủ để tương tự : Mở/Bật Nén
Diễn đàn Tiêu đề Date
Lập trình web Asp.net Cách đổi Inch sang Pixel (In to Px) mà lập trình viên nên biết 2/3/20
Lập trình web Asp.net 6 bước nên lưu ý khi lập 1 website 12/4/17