![]() |
Web Development »
ASP.NET »
General
Beginner
License: The Code Project Open License (CPOL)
Reducing the size of ASP.NET pagesBy Aj 2010A technique to reduce the size of ASP.NET pages. |
C#, Windows, .NET, ASP.NET
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
We all know when a page is requested, ASP.NET processes the page, its server controls, and finally sends the HTML to the client-side for the browser to render it. The time taken to download the HTML at the client side depends mainly on the final size of the page. If your page is data rich, it would take a lot of time to fetch it. So here I am presenting a technique to reduce the size of ASP.NET web pages.
When you see a page's HTML source by clicking “View Source” in the browser, you could see that there are a lot of white space on the left side of each line on the HTML. This is actually a waste. Try saving the HTML page to your desktop and notice its size. Then delete all the spaces on the left side and then check its size. You could see that the size of the page reduces considerably, sometimes more than 50%!! (watch the View Source of Orkut in a browser).
Here is a technique to achieve the same.
System.Web.UI.Page class.public class MyPageBase : System.Web.UI.Page
{
}
System.Web.UI.Page in all your web pages (.aspx.cs) with MyPageBase. This means that now all your web pages are deriving from MyPageBase.Render function in this class:public class MyPageBase : System.Web.UI.Page
{
protected override void Render(HtmlTextWriter writer)
{
}
}
public class MyPageBase : System.Web.UI.Page
{
protected override void Render(HtmlTextWriter writer)
{
base.Render(htmlwriter);
string html = htmlwriter.InnerWriter.ToString().Trim();
if (ConfigurationManager.AppSettings["OptimizeHtmlOutput"] != "0")
{
bool isAsync = !html.StartsWith("<");
if (!isAsync)
{
StringBuilder sb = MyPageBase._TrimHtml(html);
writer.Write(sb.ToString());
}
else
{
StringBuilder sb = new StringBuilder();
int startIx = 0;
while (true)
{
int x = html.IndexOf("|updatePanel|", startIx);
if (x > -1)
{
int xS = html.LastIndexOf("\r\n", x);
xS = (xS < 0 ? 0 : xS);
int xE = html.IndexOf("\r\n", x) + 2;
string header = html.Substring(xS, xE - xS);
header = header.Trim().TrimStart('|', ' ');
string sLen = header.Substring(0, header.IndexOf('|'));
int cLen = int.Parse(sLen);
string content = html.Substring(xE - 2, cLen);
content = MyPageBase._TrimHtml(content).ToString().Trim();
startIx = xE - 2 + cLen;
cLen = content.Length + 4;
header = (xS > 0 ? "|" : string.Empty) +
cLen.ToString() +
header.Substring(header.IndexOf('|'));
sb.AppendLine(header);
sb.AppendLine(content);
}
else
{
string stateData = html.Substring(startIx);
sb.Append(stateData.Trim());
writer.Write(sb.ToString());
break;
}
}
}
}
else
{
writer.Write(html);
}
}
}
private static StringBuilder _TrimHtml(string source)
{
StringBuilder sb = new StringBuilder();
source = source.Trim();
using (StringReader sr = new StringReader(source))
{
string data = string.Empty;
while (data != null)
{
data = sr.ReadLine();
if (data != null)
{
data = data.TrimStart(' ', '\t');
if (data != string.Empty) sb.AppendLine(data);
}
}
}
return sb;
}
}
ConfigurationManager.AppSettings["OptimizeHtmlOutput"] != "0". You can define a key in the appsettings section of the web.config file to enable or disable this feature. Any value other than “0” will enable optimization.
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 23 Nov 2009 Editor: Smitha Vijayan |
Copyright 2009 by Aj 2010 Everything else Copyright © CodeProject, 1999-2010 Web21 | Advertise on the Code Project |