50 Tips to Boost ASP.NET Performance – Part I

When we are looking to optimize the performance of  web applications we should keep in mind about Memory Load, Processor Load and Network Bandwidth. Here are 50  best practices to improve the performance and scalability of ASP.NET applications.

1. Page.IsPostBack Property

Keep code which only needs to be loaded once inside an IsPostBack block.

if(!IsPostBack)
{
BindDropDownList();
LoadDynamicControls();
}

As a result there will be no unnecessary database hits and server processing.

2. Enable Buffering

A buffer is a region in main memory to store temporary data for input and output .Data retrival from memory is faster than data retrieval from disk. We should leave buffering on unless there is any specific reason to turn it off. By default buffering is enable.

3. Remove unused HttpModules

There may be lot of HttpModules in Machine.Config that are not actually required for a particular application. In this scenario we should remove those unused HttpModules from application specific web.config file.

4. Trim Page Sizes

Reduce page size by removing any unnecessary space and tab characters from the page. As a result network traffic will be reduced.

5. Use a CDN

Not a performance tip exclusive to ASP.NET but an important step in speeding up a site is to use a Content Delivery Network (CDN) . CDN’s minimize the latency site visitors experience when they request a larger file from a data center that is located geographically far away. CDN’s cache files at numerous edge locations around the world to minimize latency.
If you are using Azure consider using the Windows Azure CDN , Amazon’s CloudFront cheap and easy to integrate into a website (if you happen to have a WordPress blog you can  integrate S3 and CloudFront into WordPress)

6. Server.Transfer and Response.Redirect

“To perform client side redirection in ASP.NET, users can call Response.Redirect and pass the URL. When Response.Redirect is called, the server sends a command back to the browser telling it to request the page redirected to it.  An extra roundtrip happens, which hit the performance.  We can send information from the source page by using a query string.  There is a limitation on the length of a query string; it cannot be used to pass large amounts of data over the wire.

To perform server-side redirection, users can use Server.Transfer.  As the execution is transferred on the server, Server.Transfer does not require the client to request another page.  In Server.Transfer, by using HttpContext we can access the source page’s items collection in target page.  The drawback of using this method is that the browser does not know that a different page was returned to it.  It displays the first page’s URL in the browser’s address bar.  This can confuse the user and cause problems if the user tries to bookmark the page.  Transfer is not recommended since the operations typically flow through several different pages.”

7. Precompiling

Precompiling an ASP.NET Web site provides faster initial response time for users because pages do not have to be compiled the first time they are requested. This is particularly useful for large Web sites that are updated frequently. In order to achieve that we can use ASP.NET Compilation Tool (Aspnet_compiler.exe).

8. Session State Management

Efficient state management helps to boost the performance and scalability of application. It is not advisable to keep large objects in a session variable and it is optimal to make disable session state whenever it is not required. We can turn session state off either at the Page level or at the application level using the  config file.

9. ViewState

By default the viewstate is enabled for applications and we should disable it whenever it is not required. Otherwise it will increase the page size. Every byte added to a web page by enabling its viewstate causes two bytes of network traffic – one in each direction. Disable

view state in any of the following scenarios:

(i)  A readonly page where there is no user input.

(ii) A  page that does not postback to the server

(iii) A page which requires rebuilding server controls on each post back without checking the post back data.

It is best practice to turn ViewState off at the application level and then enable it as required at the page or even control level.

10. Caching

Probably the number one performance tip is to use caching. In order to store static data caching is ideal one. There are different types of caching: Page output caching, Page fragment caching data caching and we have to select the correct type as per requirement.
In almost all scenarios at least a part of a page can be cached.

11. Locking and Shared Resources

Acquire shared resources late and release them as early as possible.  Avoid locking unless absolutely necessary.  Do not set lock on the “this;” it is better to use a private object to lock on as follows:

public Class Test
{
private stativ readonly objLock=new Object();
public static Test Singleton
{
lock(ObjLock)
{
return new test();
}
}

Continues…

ASP.NET MVC Routing Tutorial – Part I

In this tutorial we will take a look at ASP.NET MVC Routing which is a very powerful method of handling URL’s in web apps. It should be noted at the outset that R0uting is not exclusive to MVC and can also be used in ASP.NET 4.0 – see ASP.NET 4.0 URL Routing Tutorial for more details (most of the detail provided below is applicable to ASP.NET Routing as well as MVC Routing,  although the implementation is slightly different)

ASP.NET MVC Routing serves two main purposes:

  • Matching incoming requests and mapping them to a controller action.
  • Constructing outgoing URLs which correspond to controller actions.

ASP.NET MVC Routing compared to URL Rewriting

Both Routing and URL ReWriting can create search engine friendly  URLs. A key difference, however, is that URL Rewriting is an approach which focuses on a single page at a time. Most ASP.NET URL rewriting schemes rewrite a URL for one page to be handled by another. For example ,  /product/cars.aspx might be rewritten as: /products.aspx?id=65 .

Routing, by contrast. is a resource-centric view of URLs. A URL represents a resource which is not necessarily a web page. With Routing, this resource is a block of code that executes when an incoming request matches the route. A route determines how a request is dispatched based on characteristics of a URL.

Another difference is that Routing is bidirectional as it also helps in generating  URLs by using the same rules used to match incoming URLs. However, ASP.NET Routing never actually rewrites a URL, the URL that a user requests in the browser is the same URL the application sees throughout the request lifecycle.

Defining Routes

An ASP.NET MVC app needs a minimum of one route to define how the app should handle requests, complex apps will likely have numerous routes.  Route definitions begin with a URL that specifies a pattern the route will match. As well as the route URL, routes may also specify a default value and constraints for parts of the URL. This provides very tight control over how a route matches the incoming request URLs.

Route URLs

In the Application_Start method of the Global.asax.cs file in a ASP.NET MVC Web Application project, there is a call to the RegisterRoutes method. This method is the place where all the routes for the app are registered.

For this tutorial we can remove all the routes in there  and enter a single very simple route:
Continues…