Subscribe Now

Trending News

Bài viết

ASP.NET, Lập trình

Nén GZIP với website ASP.NET 4.5 – IIS 8

Nén GZIP với website ASP.NET 4.5 – IIS 8

Nén Gzip sẽ làm giảm thiểu tối đa thời gian phản hồi bằng cách giảm dung lượng tải về từ giao thức HTTP.  Nó có thể nén các thành phần tĩnh trên website như CSS, Javascript, HTML nhưng trên lý thuyết và 1 số trường hợp, nó có thể làm việc cùng với XML và JSON. Các thành phần khác như hình ảnh, tài liệu PDF..v.v..có thể không cần sử dụng gzip vì bản thân nó đã được nén sẵn.

Gzip giảm thời gian đáp ứng các yêu cầu của client bằng cách nén các thông tin trả lời và giảm đươc 70% kích thước. Hầu hết 90% các giao tiếp trên Internet giữa các trình duyệt và máy chủ web đều sử dụng Gzip. Đối với các website viết bằng php sử dụng Sever Apache việc áp dụng sẽ khác, ở đây ta chỉ tìm hiểu cách áp dụng gzip thế nào đối với một website ASP.NET

Bước 1: Tạo file Global.asax

Tạo file Global.asax

Tạo file Global.asax

Bước 2: Viết code trong file Global.asax

Trong file Global.asax.cs các bạn thêm vào đoạn code:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    HttpApplication app = sender as HttpApplication;
    string acceptEncoding = app.Request.Headers["Accept-Encoding"];
    Stream prevUncompressedStream = app.Response.Filter;
 
    if (app.Context.CurrentHandler == null)
        return;
 
    if (!(app.Context.CurrentHandler is System.Web.UI.Page ||
        app.Context.CurrentHandler.GetType().Name == "SyncSessionlessHandler") ||
        app.Request["HTTP_X_MICROSOFTAJAX"] != null)
        return;
 
    if (acceptEncoding == null || acceptEncoding.Length == 0)
        return;
 
    acceptEncoding = acceptEncoding.ToLower();
 
    if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
    {
        // deflate
        app.Response.Filter = new DeflateStream(prevUncompressedStream,
            CompressionMode.Compress);
        app.Response.AppendHeader("Content-Encoding", "deflate");
    }
    else if (acceptEncoding.Contains("gzip"))
    {
        // gzip
        app.Response.Filter = new GZipStream(prevUncompressedStream,
            CompressionMode.Compress);
        app.Response.AppendHeader("Content-Encoding", "gzip");
    }
}

Bước 3: Viết code trong web.config

Sau đó trong web.config các bạn thêm vào trong <system.Webserver>…</system.Webserver> đoạn code:

<httpCompression>
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
  <dynamicTypes>
    <add mimeType="text/*" enabled="true"/>
    <add mimeType="message/*" enabled="true"/>
    <add mimeType="application/javascript" enabled="true"/>
    <add mimeType="*/*" enabled="false"/>
  </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true"/>
    <add mimeType="message/*" enabled="true"/>
    <add mimeType="application/javascript" enabled="true"/>
    <add mimeType="*/*" enabled="false"/>
  </staticTypes>
</httpCompression>

Sau khi thực hiện nén GZIP, các bạn có thể truy cập vào checkgzipcompression.com để kiểm tra, chúc các bạn thành công!

Bài viết liên quan

Theo dõi
Thông báo của
guest

4 Góp ý
Mới nhất
Cũ nhất Được bỏ phiếu nhiều nhất
Phản hồi nội tuyến
Xem tất cả bình luận
Lam phuong nam
Lam phuong nam
3 năm trước

Sao mình làm theo bạn chạy thu web ở local (ở máy tính cá nhân của mình) thì báo lỗi :
The following errors occurred while attempting to load the app.
– No assembly found containing an OwinStartupAttribute.
– No assembly found containing a Startup or [AssemblyName].Startup class.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of “false” in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.

Psc
Psc
3 năm trước

Cách tạo file Global.aspx.cs như thế nào ạ hay nó là Global.aspx thế ạ