» » Hướng dẫn nén Gzip tăng tốc cho website Asp.net

Hướng dẫn nén Gzip tăng tốc cho website Asp.net

Ở đây chúng ta chỉ tìm hiểu cách áp dụng Gzip thế nào đối với một website Asp.net. Đối với các website viết bằng php sử dụng Sever Apache việc áp dụng sẽ khác.
Nén Gzip hiện tại đang được áp dụng rất phổ biến và được cho là tối ưu nhất để giảm dung lượng website. Né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.

Sử dụng Gzip trong Global.asax

Cách mà tôi thường dùng nhất để nén Gzip cho website và rất hiệu quả đó là nén Gzip trong Global.asax. Đầu tiên trong thư mục App_Code các bạn tạo thêm một class có tên GlobalGZip.cs với nội dung như sau:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.IO.Compression;
 
/// <summary>
/// Summary description for GlobalGZip
/// </summary>
public class GlobalGZip : System.Web.HttpApplication
{
    public GlobalGZip()
    {
        InitializeComponent();
    }
 
    private void InitializeComponent()
    {
        this.PostReleaseRequestState += new EventHandler(Global_PostReleaseRequestState);
    }
 
    /// <summary>
    /// Event handler for PostReleaseRequestState
    /// </summary>
    private void Global_PostReleaseRequestState(object sender, EventArgs e)
    {
        string contentType = Response.ContentType; // Get the content type.
 
        // Compress only html and stylesheet documents.
        if (contentType == "text/html" || contentType == "text/css")
        {
            // Get the Accept-Encoding header value to know whether zipping is supported by the browser or not.
            string acceptEncoding = Request.Headers["Accept-Encoding"];
 
            if (!string.IsNullOrEmpty(acceptEncoding))
            {
                // If gzip is supported then gzip it else if deflate compression is supported then compress in that technique.
                if (acceptEncoding.Contains("gzip"))
                {
                    // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
                    Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress);
                    Response.AppendHeader("Content-Encoding", "gzip");
                }
                else if (acceptEncoding.Contains("deflate"))
                {
                    // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
                    Response.Filter = new DeflateStream(Response.Filter, CompressionMode.Compress);
                    Response.AppendHeader("Content-Encoding", "deflate");
                }
            }
        }
    }
}

Tiếp theo ta chỉ cần thêm vào file Global.asax của chúng ta như sau, đó là Inherits lớp chúng ta mới tạo vào.

<%@ Application Language="C#" Inherits="GlobalGZip"%>

Kiểm tra tính năng Gzip của website

Sau khi tiến hành tích hợp tính năng Gzip vào website, chúng ta có thể sử dụng một số công cụ sau để kiểm tra 


su-dung-nen-gzip1.gif

2. Vào Trang pagespeed của google: http://developers.google.com/speed/pagespeed/insights/

su-dung-nen-gzip2.gif

Nguồn: SEAGEM.VN