Tips and Tricks for Effectively Using ASP.NET

Matt Perdeck

December 2010

ASP.NET Site Performance Secrets

ASP.NET Site Performance Secrets

Simple and proven techniques to quickly speed up your ASP.NET website

  • Speed up your ASP.NET website by identifying performance bottlenecks that hold back your site's performance and fixing them
  • Tips and tricks for writing faster code and pinpointing those areas in the code that matter most, thus saving time and energy
  • Drastically reduce page load times
  • Configure and improve compression – the single most important way to improve your site's performance
  • Written in a simple problem-solving manner – with a practical hands-on approach and just the right amount of theory you need to make sense of it all


        Read more about this book      

(For more resources on ASP.Net, see here.)

It is good to monitor the performance of your site continuously
Tip: The performance of your website is affected by both the things you control, such as code changes, and the things you cannot control such as increases in the number of visitors or server problems. Because of this, it makes sense to monitor the performance of your site continuously. That way, you find out that the site is becoming too slow before your manager does.

It is important to reduce the "time to first byte"
Tip: The "time to first byte" is the time it takes your server to generate a page, plus the time taken to move the first byte over the Internet to the browser. Reducing that time is important for visitor retention—you want to give them something to look at, and provide confidence that they'll have the page in their browser soon. It involves making better use of system resources such as memory and CPU.

Caching is one of the methods used to improve website performance
Tip: Caching allows you to store individual objects, parts of web pages, or entire web pages in memory either in the browser, a proxy, or the server. That way, those objects or pages do not have to be generated again for each request, giving you:

  • Reduced response time
  • Reduced memory and CPU usage
  • Less load on the database
  • Fewer round trips to the server, when using browser or proxy caching
  • Reduced retrieval times when the content is served from proxy cache, by bringing the contents closer to the browser


Building projects in the Release mode is a good practice
Tip: If your site is a web-application project rather than a website, or if your website is a part of a solution containing other projects, be sure to build your releases in the release mode. This removes debugging overhead from your code, so it uses less CPU and memory.

It is important to reduce Round Trips between browser and server
Tip: Round trips between browser and server can take a long time, increasing wait times for the visitor. Hence it is necessary to cut down on them. The same goes for round trips between web server and database server.

Permanent redirects
Tip: If you are redirecting visitors to a new page because the page is outdated, use a permanent 301 redirect. Browsers and proxies will update their caches, and search engines will use them as well. That way, you reduce traffic to the old page.
You can issue a 301 redirect programmatically:

Response.StatusCode = 301;
Response.AddHeader("Location", "NewPage.aspx");

For .NET 4 or higher:



Hotlinking should be avoided
Tip: Hotlinking is the practice of linking to someone else's images on your site. If this happens to you and your images, another web master gets to show your images on their site and you get to pay for the additional bandwidth and incur the additional load on your server. A great little module that prevents hot linking is LeechGuard Hot-Linking Prevention Module at

Session state is taking too much memory
Tip: If you decide that session state is taking too much memory, here are some solutions.

  • Reduce session state life time
  • Reduce space taken by session state
  • Use another session mode
  • Stop using session state


Tip: ASP.NET disables all output caching if you set a cookie, to make sure the cookie isn't sent to the wrong visitor. Since setting cookies and proxy caching also don't go together performance-wise, you'll want to keep setting the number of cookies to a minimum. This can be done by trying not to set a cookie on every request but only when strictly needed.

Minimizing the duration of locks
Tip: Acquire locks on shared resources just before you access them, and release them immediately after you are finished with them. By limiting the time each resource is locked, you minimize the time threads need to wait for resources to become available.

        Read more about this book      

(For more resources on ASP.Net, see here.)

Using System.Threading.Interlocked
Tip: If your protected code simply increments or decrements an integer, adds one integer to another, or exchanges two values, consider using the System.Threading. Interlocked class instead of lock. Interlocked executes a lot faster than lock, so should result in less waiting for locks.
For example, instead of the following:

lock (privateObject)

Use the following:

Interlocked.Increment(ref counter);


Avoiding head seeks
Tip: Writing bytes sequentially without moving the read/write head happens much faster than random access. If you are only writing the files and not reading them, try writing them on a dedicated disk drive using a single dedicated thread. That way, other processes won't move the read/write head on the drive.

Reducing the number of reads executed by queries
Tip: You can greatly improve the performance of your queries by reducing the number of reads executed by those queries. Look at the WHERE clauses of your queries and check if your database has indexes to support the clauses. The execution plan for the query shown by SQL Server Management Studio shows the number of reads executed by the query.

Unused indexes
Tip: A drawback of indexes is that they need to be updated when the data itself is updated, causing delays. They also take storage space. If an index slows down updates but is hardly used for reading, you're better off dropping it.

Keeping records short
Tip: Reducing the amount of space taken per table record speeds up access. Records are stored in 8-KB pages on disk. The more records fit on a page, the fewer pages SQL Server needs to read to retrieve a given set of records.

Using SQL Server's Full Text Search feature
Tip: If you use LIKE in your queries with a wildcard, then unless the wildcard starts with constant text, SQL Server will not be able to use any index on the column, and so will do a full-table scan instead.
To improve this situation, consider using SQL Server's Full Text Search feature. This automatically creates an index for all words in the text column, leading to much faster searches.

Replacing cursors with set-based code
Tip: If you use cursors, consider replacing them with set-based code. Performance improvements of a 1000 times are not uncommon. Set-based code uses internal algorithms that are much better optimized than you could ever hope to achieve with a cursor.

Reducing space taken by ASP.NET Ids
Tip: When ASP.NET renders server controls such as Images and Labels, it also renders their ASP.NET IDs. These IDs can be quite lengthy. You can remove the Ids programmatically after you're done with them by setting them to null.

Using shorter URLs
Tip: Long URLs not only take more space on the page, they also take more space in the request message going to the server. Shorter URLs make for a more responsive website. There may be good reasons to use longer URLs, such as a meaningful folder structure in a very complex website. So shorter URLs are not always better – it is a trade off.

Combining images
Tip: It takes far less time and bandwidth to receive one slightly bigger image than to receive lots of small ones. This can be done by using a technique called CSS Sprites.
CSS Sprites involves a way to combine images into a single image, while still showing the original images individually on the page.

Using JavaScript code on the browser
Tip: One approach to improving page performance is to shift functionality from the server to the browser. Instead of calculating a result or validating a form in C# on the server, you use JavaScript code on the browser. As a result, fewer user actions require the browser to send a request to the server and wait for the response, greatly improving responsiveness. You also reduce the load on the server.

Removing unused code
Tip: Because JavaScript code is separate from the HTML user interface elements it supports, it is easy to forget to remove code when it is no longer needed. As a result, your JavaScript files may become bloated with unused code. So it is always better to get rid of such code.


This article took a look at some of the tips and tricks for using ASP.NET.

Further resources on this subject:

You've been reading an excerpt of:

ASP.NET Site Performance Secrets

Explore Title