How to compress response in with example

Category > ASP.NET || Published on : Monday, December 14, 2015 || Views: 4059 || compress http response in compress http response compress http


Here Pawan Kumar will explain how to compress http response in for improving performance.


In previous post I have explained How to make How to make blinking/flashing text with CSS3 and jQuery, How to replace number from a string in C#, How to create draw a doughnut chart using ChartJS in detailed Example, Code Snippet for Deleting a SQL Server stored procedure, Bootstrap style DropDownList example in ASP.Net, Set DropDownList value based on text/value in jQuery, and many more articles.

Now I will explain How to How to compress response in with example

So follow the steps to learn How to compress response in with example

Step 1: First, We start by creating an Empty web site in Visual Studio .NET 2013.

Step 2: Add a new web page name "Default.aspx" with the following codes:-

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="grdEmp" runat="server">

Step 3: Add the following codes in code behind of default.aspx

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)
            grdEmp.DataSource = Person.Persons;

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
            return new List<Person>()
                    new Person(){FirstName="Raj",LastName="Singh",Phone=1213123,Email=""},
                    new Person(){FirstName="Shankar",LastName="Singh",Phone=1213123,Email=""},
                    new Person(){FirstName="Sonu",LastName="sharma",Phone=1213123,Email=""},

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

Step 4: Now check the response of the web page using Mozilla firefug :-

Step 5:  For compression we have to add global.asax file to the website  and write the following codes:-

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

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
        // Code that runs on application startup

    void Application_End(object sender, EventArgs e) 
        //  Code that runs on application shutdown

    void Application_Error(object sender, EventArgs e) 
        // Code that runs when an unhandled error occurs


    void Session_Start(object sender, EventArgs e) 
        // Code that runs when a new session is started


    void Session_End(object sender, EventArgs e) 
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    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;


Step 4: Run the application by pressing F5

Step 5: Screenshot will be shown below:-


So, In this tutorial we have learned, How to compress response in with example

Download Source Codes