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.
So let’s move on to the implementation details.
1. Turn off View state if not required:
<%@ 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:
The code below demonstrates the same.
<asp:ScriptManager ID="scriptmanager1" runat="server" ScriptMode="Release"></asp:ScriptManager>
<system.web.extensions> <scripting> <scriptResourceHandler enableCompression="true" enableCaching="true"/> </scripting> </system.web.extensions>
4] Reducing asp.net page size:
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
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:
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.