How to compress response in asp.net

In this post, I will show you how to compress HTTP response in asp.net for improving performance. Before going into details, let’s create a simple website with single page Default.aspx and add the following code into it Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grdPerson" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            grdPerson.DataSource = Person.Persons;
            grdPerson.DataBind();
        }
    }
}


[Serializable]
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Phone { get; set; }
    public string Email { get; set; }
    public static List<Person> Persons
    {
        get
        {
            return new List<Person>()
                {
                    new Person(){FirstName="Santosh",LastName="Singh",Phone=1213123,Email="santosh@gmail.com"},
                    new Person(){FirstName="Ashok",LastName="Singh",Phone=1213123,Email="ashok@gmail.com"},
                    new Person(){FirstName="Bill",LastName="Gates",Phone=1213123,Email="bill@gmail.com"},
                };
        }

    }
    public override string ToString()
    {
        return string.Format(@"FirstName {0} & LastName {1} Phone {2} Email {3}", this.FirstName, this.LastName, this.Phone, this.Email);
    }
}

Now, press F5 and see the response result in FireBug.You will see the response result in something like below.

Note down the Content-Length size(in my case is 1720) Let’s compress the response.For this right click on website ,and add Global.asax file.Inside this file add a method named {alertInfo}

void Application_BeginRequest(object sender, EventArgs e)
{

}

Inside it, paste the following code and see the result in FireBug.

 void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext incoming = HttpContext.Current;
        string oldpath = incoming.Request.Path.ToLower();
        incoming.Response.Filter = new System.IO.Compression.GZipStream(incoming.Response.Filter, System.IO.Compression.CompressionMode.Compress);
        HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip");
        HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true;

    }

Check the Content-Length property after compression. Note:For compress/decompression I am using .net framework class [GZipStream]

إرسال تعليق

Please do not post any spam link in the comment box😊

أحدث أقدم

Blog ads

CodeGuru