Click here to Skip to main content
15,891,649 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a very simple data entry page with a few fields. When the page submits, it sends a request of about 2500 bytes which I feel is very high. Ideally it should be less than 1500 bytes so that the data can be transmitted in one packet.
I would like to know how can I minimize the request size? I am not storing cookies/sessions on the page.
I read Minimize request overhead[^] but there is not much i can implement from it.

Any help would be greatly appreciated.

[Update]
Thanks for the answer(Ilya Builuk).
Sorry that I forgot to mention I have already disabled ViewState for the page (the ViewState anyways was not much heavy for my page).

Regards
Ankur
Posted
Updated 11-Mar-10 2:52am
v3

You can compress your ViewState data:

1. Add SharpZipLib assembly in your project.
2. Add this code in your App_Code:

using System;
using System.IO;
using System.Web.UI;

using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
 
namespace PackViewState
 {
 	public class Page : System.Web.UI.Page
 	{
		public byte[] Compress(byte[] bytes)
 		{
 			MemoryStream memory = new MemoryStream();
 			Stream stream = new DeflaterOutputStream(memory,
 				new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
 				ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION), 131072);
 			stream.Write(bytes, 0, bytes.Length);
 			stream.Close();
 			return memory.ToArray();
 		}
 
 		public byte[] Decompress(byte[] bytes)
 		{
 			Stream stream = new InflaterInputStream(new MemoryStream(bytes));
 			MemoryStream memory = new MemoryStream();
 			int totalLength = 0;
			byte[] writeData = new byte[4096];
 			while (true) 
 			{
 				int size = stream.Read(writeData, 0, writeData.Length);
				if (size > 0) 
 				{
 					totalLength += size;
 					memory.Write(writeData, 0, size);
				} 
 				else 
					break;
 			}
 			stream.Close();
 			return memory.ToArray();
 		}
 
 		protected override object LoadPageStateFromPersistenceMedium()
 		{
 			string vState = this.Request.Form["__VSTATE"];
			byte[] bytes = System.Convert.FromBase64String(vState);
 
 			bytes = this.Decompress(bytes);

			LosFormatter format = new LosFormatter();
			return format.Deserialize(System.Convert.ToBase64String(bytes));
		}

 		protected override void SavePageStateToPersistenceMedium(object viewState)
  		{
			LosFormatter format = new LosFormatter();
			StringWriter writer = new StringWriter();
			format.Serialize(writer, viewState);
			string viewStateStr = writer.ToString();

			byte[] bytes = System.Convert.FromBase64String(viewStateStr);
			bytes = this.Compress(bytes);
 
			string vStateStr = System.Convert.ToBase64String(bytes);

			RegisterHiddenField("__VSTATE", vStateStr);
		}
 	}
 }


3. Inherit your page from PackViewState.Page:

public class MyPage: PackViewState.Page
{
}
 
Share this answer
 
Hi Ankur, answering this one because the question status is not closed(I know you don't need this answer now :) ).

You may try Yslow[^]

Best Practices for Speeding Up Your Web Site[^]

How To Reduce Page Size In ASP.NET[^]
 
Share this answer
 
Comments
Ankur\m/ 2-Jan-11 23:27pm    
Thanks Raja.
I am already using YSlow and many other tools like that. :)

Well, I have already taken care of the performance by considering the best practices.
My question was if we can control the size of the request? The more web-controls you have on a page, more is it's request size.
My problem actually was that the MTU on router was set to 1496 instead of a default 1500. So if I could control the packet size of the request, I wouldn't have to bother about the network issues.

Well I solved the issue. Thanks again!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900