Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Tip/Trick

Ten Simple Tips to Improve Performance of an ASP.NET Website

Rate me:
Please Sign up or sign in to vote.
4.71/5 (23 votes)
11 Jan 2015CPOL4 min read 29.8K   31   9
Tips to improve the performance of ASP.NET web applications

Introduction

In this tip, we will look at various aspects of improving the performance of ASP.NET web applications.

Performance is an important aspect of the modern day web application development. Here are some tips that you can consider while making a better performing website.

1. Upgrade Your ASP.NET Framework

Check your .NET framework. If you can upgrade your site to use .NET 4.5, then it has some great performance optimizations. .NET 4.5 has a new Garbage Collector which can handle large heap sizes (i.e., tens of gigabytes). Some other improvements are Multi-core JIT compilation improvements, and ASP.NET App Suspension. These optimizations do not require code changes.

2. Caching

  1. Use output caching – Use output caching for regularly used views or pages which have no dynamic updates. The easiest way to implement cache on MVC view is to add an [OutputCache] attribute to either an individual controller action or an entire controller class. Here is a controller action Index() that will be cached for 15 seconds.
    C#
    [OutputCache(Duration = 15, VaryByParam = "None")]
    public ActionResult Index(string Id)
    {
    
    }
  2. Use Data caching - Reduces the database or the disk I/O by caching the regularly used data to in-memory cache. This avoids repeated queries for data, and it can improve performance and scalability. In addition, caching makes data available when the data source is temporarily unavailable. The .NET Framework provides classes that enable you to use caching facilities in ASP.NET applications. These classes are defined in the System.Runtime.Caching namespace.

3. Always keep CSS and JavaScript External

Never add any JavaScript or inline style information within the views. That would regenerate the view each time and you would miss out on the benefits of the Caching. Hence always keep JS and CSS as separate files and add them as links in the view.

4. File Compression

There are often requests to the web server with lot of static content. These contents can be compressed thereby reducing the bandwidth on requests. The following setting is only available in II7 and later.

C#
<configuration>  
    <system.webServer>    
        <urlCompression doStaticCompression="true" doDynamicCompression="true" />  
    </system.webServer> 
</configuration> 

The urlCompression name sounds strange but it is not really the compressing of URLs. It means compressing or gzipping the content that is sent to the browser. By setting to true/enabling, you can gzip content sent to the browser while saving lots of bandwidth.

5. Bundling and Minification

The custom CSS files and the JavaScript files should be bundled into a single large file (reduces the number of HTTP requests) and also minified (reduces the size of the data transferred over the wire).

6. CDN (Content Delivery Network)

All the 3rd party JavaScript files such as JQuery, Knockout should always use the CDN instead of the web application server. CDN Servers are dedicated to deliver the static content and is always faster than your own host. There is a very high probability that the client (browser) would have already cached the JavaScript as part of other web application since most of them use the same CDN URL.

7. Control Image Requests

There are couple of ways to do this.

  1. Image sprite - With image sprite, you can combine multiple different images into a single large image. Then use CSS to reposition those images within the site.
  2. Base64 Data URIs - With this option, you would never make any requests to the server to obtain any images.

8. Script Rendering Order

Move the script tags <script> to the very bottom of the page. The reason this is important is because during the rendering, when the browser comes across a <script> tag, it stops to process the script and then moves ahead. If you put the script tags at the bottom of the page, the page/HTML will render faster and the scripts can execute after the DOM elements have loaded. Sometimes moving the script to the bottom of the page is not possible as some DOM elements or CSS may depend on these scripts, so they can be rendered. In such cases, you could move those scripts further up the page.

There are some other ways:

  1. defer attribute
    C#
    <script src="some.js" defer>
    
    </script>

    Using the defer attribute, you can specify the script not to run until the page has been fully loaded.

  2. async attribute
    C#
    <script src="some.js" async>
    
    </script>

    Using the async tag, the scripts will be run asynchronously, as soon as it is available.

9. Removing Default HTTP Modules in ASP.NET

ASP.NET has many http modules waiting for request to be processed and would go through the entire pipeline even if it’s not configured for your application.

All the default modules will be added in the machine.config place in“$WINDOWS$\Microsoft.NET\Framework\$VERSION$\CONFIG directory. One could improve the performance by removing those modules which you wouldn’t require.

10. Compile in Release Mode

Always set the build configuration to release mode for the website.

In this tip, we looked at some of the approaches that you can take to make optimized and better performing web sites. This included, reducing number of requests to the server, changes to the .NET framework, and compressing techniques.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Global Infonet Inc
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Santosh K. Tripathi12-Jan-15 17:33
professionalSantosh K. Tripathi12-Jan-15 17:33 
SuggestionDetails Pin
Ciprian Beldi11-Jan-15 23:17
Ciprian Beldi11-Jan-15 23:17 
GeneralRe: Details Pin
Member 1033810512-Jan-15 8:40
Member 1033810512-Jan-15 8:40 
QuestionAlways add external JS/CSS Pin
Muhammad Hassan Tariq11-Jan-15 21:05
professionalMuhammad Hassan Tariq11-Jan-15 21:05 
GeneralRe: Always add external JS/CSS Pin
Member 1033810512-Jan-15 8:32
Member 1033810512-Jan-15 8:32 
Quote:
when our page has few lines of script or css code, then i dont think its better to add eternal js/css

You're right, when there is only a small amount of javascript or CSS then putting it in an external file won't matter. But having larger amounts of either in external files is beneficial not because of the order of execution but because the browser will grab those files and cache them, never having to fetch them again unless the cache is cleared (or newer versions appear).

If the same js or css is embedded in every page then the browser will be fetching content that it should already have - except you've forced it to fetch it as part of multiple pages.

Additionally, there is some (very) small amount of security as most of the world has no idea how to see web source content that isn't in the page itself. When I say most of the world I am obviously not including developers, hackers or technically inclined users. Those are a miniscule fraction of the universe.
GeneralMy vote of 4 Pin
Reelix11-Jan-15 20:01
Reelix11-Jan-15 20:01 
GeneralRe: My vote of 4 Pin
Member 1033810512-Jan-15 9:04
Member 1033810512-Jan-15 9:04 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun11-Jan-15 18:56
Humayun Kabir Mamun11-Jan-15 18:56 
GeneralMy vote of 3 Pin
DotNetCodebased11-Jan-15 13:43
DotNetCodebased11-Jan-15 13:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.