Category >
ASP.NET
|| Published on :
Monday, December 14, 2015 || Views:
10006
||
compress http response in asp.net compress http response compress http
Introduction
Here Pawan Kumar will explain how to compress http response in asp.net for improving performance.
Description
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 Asp.net 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 asp.net with example
So follow the steps to learn How to compress response in asp.net with example
Step 1: First, We start by creating an Empty asp.net 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdEmp" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
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;
grdEmp.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="Raj",LastName="Singh",Phone=1213123,Email="raj@gmail.com"},
new Person(){FirstName="Shankar",LastName="Singh",Phone=1213123,Email="shankar@gmail.com"},
new Person(){FirstName="Sonu",LastName="sharma",Phone=1213123,Email="sonu@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);
}
}
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;
}
</script>
Step 4: Run the application by pressing F5
Step 5: Screenshot will be shown below:-
Conclusion:
So, In this tutorial we have learned, How to compress response in asp.net with example
Download Source Codes