Thursday, 15 November 2012

Reduce the Size of ASP.Net Pages


Reduce the Size of ASP.Net Pages


We all know when a page is requested, ASP.NET process 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 to client side depends mainly on its final size. If your page is data rich, it would take much time to fetch it. So here I am presenting a technique to reduce the sizes of the pages.

When you see the pages html source by clicking the “View Source” in the browser, you could see that there are a lot of white spaces 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 watch its size. You could see that the size of the page reduces considerably, sometimes more than 50%!! (watch the view source of Orkut in the browser).

Here is a technique to achieve the same.

1. Create a class in the App_Code, that is deriving from “System.Web.UI.Page” class.

public class MyPageBase : System.Web.UI.Page
{
}

2. Replace “System.Web.UI.Page” from all your web pages (.aspx.cs) and put MyPageBase. Means now all your webpages are deriving from MyPageBase.

3. Override the Render function in this class

public class MyPageBase : System.Web.UI.Page
{
    protected override void Render(HtmlTextWriter writer)
    {
    }
}

4. Place the below code inside the function and also write a support function as below:


public class MyPageBase : System.Web.UI.Page

{

    protected override void Render(HtmlTextWriter writer)

    {

         using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))

        {

            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;

    }

}


Notes:
1. Notice the usage of ConfigurationManager.AppSettings["OptimizeHtmlOutput"] != "0". You can define a key in app settings section of the web.config file to enable or disable this feature. Any value other that “0” will enable the optimization.
2. This code works well in AJAX based applications as well.
Compare the size difference and performance by changing the flag. This is very beneficial especially in pages which contains a huge amount of data.

Reference:

http://forums.asp.net/t/1495712.aspx/1?Reducing+size+of+ASP+NET+pages

No comments:

Post a comment

Thank You for Your Comments. We will get back to you soon.

back to top