e-Zest members share technology ideas to foster digital transformation.

How to improve the performance of the application in ASP .Net

Written by Swapna Patne | Aug 25, 2014 2:40:45 PM

Introduction

In this blog I would like to share some of my interesting findings regarding optimizing the page rendering time and increasing the performance of the web application using C#.NET.

Before starting, it is very important to identify which part of your application requires more attention, in order to improve the website performance. So below we have some ways to identify the pages.

  1. Using VS.Net 2010 Profiler
  2. Tracing asp.net web application
  3. Extension (Firefox Firebug, Google Chrome Speed Tracer, IE9 Developer Tools)
  4. Monitoring tools like fiddler is also helpful

So let’s move on to the implementation details.

1. Turn off View state if not required:

  • There is huge amount of data which is stored in View State generated at client side in an encrypted format and increases the page size.
  • If you are not using form postback, turn off view state, by default controls will turn on viewstate and slow your site.
  • If you are using asp.net 2.0 or higher version then use asp.net controls state instead of view state. Store view state in session or database by overriding the default methods for storing view state.
  • In such cases where you need not use viewstate, disable it on your web form using the directive,

<%@ Page EnableViewState="false" %>

2. Turn off Session State, if not required:

Since ASP.NET manages session state by default, you end-up paying the cost in memory, even if you don't use it.

In case of static pages session state is not required and in such a case disable it on your web form using the directive,
<@%Page EnableSessionState="false"%>

In case of the need to retrieve the data without any CRUD transactions on it, make the session state “read only” by using the directive,
<@%Page EnableSessionState ="ReadOnly"%>

3] Compression for script files:

  • Make use of JQuery instead of Ajax Control toolkit. But if you are using Ajax Toolkit in web applications Master Page, then include compression code for ScriptResource.axd as this will encrypt the script resource file and it generates huge encrypted data on each request.

The code below demonstrates the same.

    • Master Page: The tag used below is used if we are using Ajax Tool Kit in the application.
<asp:ScriptManager ID="scriptmanager1" runat="server" ScriptMode="Release"></asp:ScriptManager>
    • Web Config File :
<system.web.extensions>
      <scripting>
            <scriptResourceHandler enableCompression="true" enableCaching="true"/>
      </scripting>
</system.web.extensions>

4] Reducing asp.net page size:

  • To reduce the page size, implement compression code in global.asax file, which compress webpages with “gzip” and reduces the bandwidth consumption of the website. All web browsers support “gzip” compression, and it is recommended to use this to reduce the size of webpages which in turn can dramatically reduce the download time. Also the gzipping of pages is done at the server after the page is rendered and sent back to the client’s browser.

The code below demonstrates the same.

Add the reference System.IO.Compression.dll in global.asax as below:

<%@ Import Namespace="System.IO.Compression" %>

<script Runat="server">
    void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;
        string acceptEncoding = app.Request.Headers["Accept-Encoding"];
        Stream prevUncompressedStream = app.Response.Filter;

        if (!(app.Context.CurrentHandler is Page ||
            app.Context.CurrentHandler.GetType().Name == "SyncSessionlessHandler") ||
            app.Request["HTTP_X_MICROSOFTAJAX"] != null)
            return;

        if (acceptEncoding == null || acceptEncoding.Length == 0)
            return;

        acceptEncoding = acceptEncoding.ToLower();

    if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
        {
        // defalte
            app.Response.Filter = new DeflateStream(prevUncompressedStream,
                CompressionMode.Compress);
            app.Response.AppendHeader("Content-Encoding", "deflate");
        }
        else if (acceptEncoding.Contains("gzip"))
        {
            // gzip
            app.Response.Filter = new GZipStream(prevUncompressedStream,
                CompressionMode.Compress);
            app.Response.AppendHeader("Content-Encoding", "gzip");
        }

    }
</script>

5] Reduce number of HTTP request

  • Images: Make minimum use of Images as images are good for UI but can increase the size of the web page and lead to too many http request by increasing the load time.
  • JavaScript: Remove inline script from the web pages and write it in .Js files and place it as bottom of the application. Since many browsers stores JavaScript file in cache, so it takes some time for first request and hence rest other request becomes faster. Also combine all .Js files in single file which will reduce the http request.
  • Style Sheets: Use Separate CSS files for styling of application and combine 2 or more css files into one single file. Since many browsers stores css file in cache, it takes some time for first request and hence the other requests become faster. Also combine all css files in a single file which will reduce the http request.

6] Avoid Exceptions:

Always avoid throwing exception in the catch block as it is the heavy resource which slowdowns the web application. Instead, off handle the exception by inserting into the database and don’t code or write any condition in exception blocks and use final method to kill resources.

7] Use Paging:

Implement database paging over normal paging while displaying huge amount of data.

Implement paging to show only small subsets of data at a time so that the page loads faster.

8] Avoid Server round trip: In order to provide user faster effect and to avoid round trip to server use JQuery Ajax or Cache in the web application.

9] Deployment changes:

  • Publishing the application into Single Page assemblies will slightly increase the performance of the application.
  • Deploy setup in release mode which will increase performance, as by running in debug mode you are creating PDB’s and cranking up the timeout. Deploy Release mode and you will see the speed improvements in production environment.
  • Always set debug=”false” in web.config production environment.
  • Always set trace=”false” in web.config production environment

10) Use Client Side Scripts for validations:

User Input must be thoroughly validated at client side by using JQuery, JavaScript or asp.net regular expressions before processing to avoid overhead and possible injections to your applications.

So friends, there are many more such things, however I really feel that these are the most critical aspects we need to look at in order to speed up and improve performance of the web applications.