Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4762 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Reduce ASP.NET Page Output Size

Reduce ASP.NET Page Output Size

Article posted by Bibhas.paul on 2/20/2010 | Views: 8285 | Category: ASP.NET | Level: Beginner red flag


Reduce ASP.NET Page Output Size

Download


 Download source code for Reduce ASP.NET Page Output Size


Introduction

There are several techniques available to reduce ASP.NET page output size. Today we will discuss two interesting techniques among them.

HTTP Compression

This is a cool technique to reduce page size by compressing HTTP response stream. This feature is available as a part of IIS6.0 and onwards. Generally output of ASP.NET page is in text format which travels from IIS to client browser. Don’t get disappointed, HTTP Compression features can be achieved in ASP.Net application hosted in IIS 5.1 also. Only difference is, we have to write little line of codes instead. Do the following steps in “Global.asax” to encode the page output.

Step1: Include the namespace “System.IO.Compression

Step2: Write following function to see, whether HTTP compression is supported by client browser.

public bool IsCompressionAllowed()

{
   //Browser supported encoding format
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];

if (!string.IsNullOrEmpty(AcceptEncoding))

{
if(AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate"))
return true; ////If browser supports encoding
}

return false;

}

Step3:  Add the event handler “Application_AuthenticateRequest and write following code.

protected void Application_AuthenticateRequest(Object sender, EventArgs e)

{
   HttpResponse Response = HttpContext.Current.Response;
   if (IsCompressionAllowed()) // If client browser supports HTTP compression

   {
      //Browser supported encoding format
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept- Encoding"];

      //if client browser supports both "deflate"
//and "gzip" compression,
Then we will consider "deflate" first , because it is
// more efficient than "gzip"
 


if (AcceptEncoding.Contains("deflate"))
{
Response.Filter = new DeflateStream
(Response.Filter, CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "deflate");
}
else
{
Response.Filter = new GZipStream(Response.Filter,CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "gzip");
}
}

// Allow proxy servers to cache encoded and
unencoded versions separately
Response.AppendHeader("Vary", "Content-Encoding");

}

Above code will compress HTTP response based on the compression format supported by browser. Now if we run a sample application and track the http response through Fiddler, we can see that, the response has come to browser in “deflate” format. See below the screen shot.

 

If we remove the compression part from Global.asax and run the same application to see HTTP Response in fiddler, we will see following. 

       

Just compare the page size. 1,765 byte vs. 106861 byte and understand how drastically the page size has come down to 1765 byte with “HTTP Compression”.

SessionPageStatePersister

ASP.NET persistence mechanism store view state data in  HiddenFieldPageStatePersister class , which in turn maintain data in a hidden text box. This mechanism is well known but specially creates problem when view state is not maintained carefully. Page with larger size of view state increase the downtime and affects the performance. Using “SessionPageStatePersister” we can store viewstate in session also. Storing view state in session drastically reduces page size and expedites performance. See below the steps involved.

Step1:  Create a page controller inherited from “Page” class

    public class PageController : Page

{
protected override PageStatePersister PageStatePersister
{
get
{
// Store view state in session
return new SessionPageStatePersister(this);
}
}
}

Step2:  Make sure all ASP.NET page must derive from above controller.

    public partial class Default : PageController
{
   protected void Page_Load(object sender, EventArgs e)
{

////////////
}

}

That’s all, if you browse your page and check the view state. You can see view state size has almost come down to half. Below is the view state of a page displaying 1000 records in a grid and each record is pertaining 5 columns.

 <input type="hidden" id="__VIEWSTATE" alue="/wEPaA8FDzhjYzdmOTg5ZTEyZGRhYxgBBQdHcmRVc2VyDzwrAAoBCAIBZJVttOvUuwS2wCkalf3GpRmGTzHN" />

 

While implementing SessionPageStatePersister we must consider following points

1.    Since viewstate is maintaining in session so numerous access to the application might cause memory crash.

2.    Viewstate info will still persist in the memory after user logs out or leaves the page. Memory will get release when session time out occurs.


 


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Bibhas Paul

Experience:8 year(s)
Home page:
Member since:Saturday, September 26, 2009
Level:Starter
Status: [Member]
Biography:Bibhas has 8 years of extensive experience in application development with exposure to business requirement study, system analysis and designing, coding ,testing,
implementation, end user training and client Interaction.

He was the part of application development team and worked for industry leading organizations like "ConocoPhillips", "Abbey National Bank" and "DHL".

Posses sound experience as a technical architect and all part of software development lifecycle.

His interest includes in Microsoft Technologies (ASP.NET 3.5 & SQL Server), Design Pattern and wide variety of internet technologies like AJAX, JQUERY etc.
>> Write Response - Respond to this post and get points
Related Posts

In this article we shall learn How to create a slide show images from the database in ASP.NET.

In this Article you can learn how to get a Selected Row Data from a DataList and display the data in textBoxes.

ModalPopupExtender is used to open a pop-up or to open a separate ASP.NET page as a pop-up

In case we have to give ability to the user to select the columns that should appear in the GridView, we can follow this approach.

I am writing this article to check how the cache of a page works in a website. I shall show all the details with example.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:50:19 AM