Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How To Improve the Performance of ASP.NET MVC Web Application

0.00/5 (No votes)
5 Sep 2009 1  
In this article, I will examine how you can improve the performance of an ASP.NET MVC application by taking advantage of the various components

Introduction

In this article, I will examine how you can improve the performance of an ASP.NET MVC application by taking advantage of the following components:

  1. Implementing Caching
  2. Implementing HTTP Compression
  3. Implementing JQuery UI library with Google Hosted Ajax Libraries
  4. By combining scripts and other resources
  5. Deploying production code in release mode
  6. Removing default HTTP modules in ASP.NET
  7. Optimizing URL generation

Implementing Caching

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 GetWeather() that will be cached for 15 seconds.

[OutputCache(Duration = 15, VaryByParam = "None")]
public ActionResult GetWeather(string Id)
{
}

To cache your entire controller, you will implement [OutputCache] attribute on an entire controller class as shown below:

[OutputCache(Duration = 15, VaryByParam = "None")] 
public class WeatherController : Controller
    {
        //
        // GET: /Weather/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetWeather(string Id)
        {
        }
   }

For more details, please read this.

You can also control the cache programmatically by implementing Cache API. The System.Web.Caching.Cache class works like a dictionary. You can add key and item pairs to the Cache class. When you add an item to the Cache class, the item is cached on the server. The following code example adds an item to the cache with a sliding expiration time of 10 minutes:

Cache.Insert("Key", "Value",
    null, System.Web.Caching.Cache.NoAbsoluteExpiration,
    new TimeSpan(0, 10, 0));

For more details, please read this.

The one limitation of the ASP.NET Cache object is that it runs in the same process as your web application. It is not a distributed cache. If you want to share the same ASP.NET Cache among multiple machines, you must duplicate the cache for each machine. In this situation, you need to use a distributed cache. To implement distributed cache, you can use the Microsoft distributed cache (code-named Velocity) with an ASP.NET MVC application. Here is a great article by Stephen Walther where he explains in detail.

You can also cache any HTTP get request in the user browser for a predefined time, if the user requests the same URL in that predefined time the response will be loaded from the browser cache instead of the server. Here is another great article about ASP.NET MVC Action Filter - Caching and Compression by Kazi Manzur where he explains in detail.

Implementing HTTP Compression

The easiest way to implement compression is to apply IIS Compression, and here is a great article that explains more in detail.  

You can apply the action filter to compress your response in your ASP.NET MVC application. A great article that explains this in detail is ASP.NET MVC Action Filter - Caching and Compression.

There are several problems with ASP.NET MVC application when deployed on IIS 6.0. Here is a solution presented by Omar AL Zabir.

There's also a port of the YUICompress for .NET on CodePlex that compresses any Javascript and Cascading Style Sheets to an efficient level.

Implementing JQuery UI library with Google Hosted Ajax Libraries

You can Build Rich client site User Interfaces with jQuery. There is a great article by Dino Esposito on Building Rich User interfaces. You should also consider using Google hosted AJAX libraries API as it will improve the site performance. Please see Test Drive of the Google Hosted Ajax Libraries.

By Combining Scripts and Other Resources

ASP.NET MVC Client-side Resource Combine is another great library which is available at CodePlex. This library requires you to organize client-side resources into separate sets, each with different configuration settings (although there's nothing stopping you from having a 1-file resource set). The resources in each set are to be minified, combined, compressed, and cached together and therefore can be requested in 1 single HTTP request. Refer to the project CodePlex page for detailed usage and binary/code download. The library uses the great YUI Compressor library for the minification part.

Deploying Production Code in Release Mode

When we develop ASP.NET applications using Visual Studio, the default value for debug attribute is true. These settings will give a poor performance in production if released in the default debug mode. So, never release your website or application with debug mode set to true. It should be set to false in web.config when moving to production.

 <compilation debug="false" />

For more details, please read this.

Removing Default HTTP Modules in ASP.NET

The following default httpModules element is configured in the root Web.config file in the .NET Framework version 2.0.

 <httpModules>
     <add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />
     <add name="Session" type="System.Web.SessionState.SessionStateModule" />
     <add name="WindowsAuthentication" 
	type="System.Web.Security.WindowsAuthenticationModule" />
     <add name="FormsAuthentication" 
	type="System.Web.Security.FormsAuthenticationModule" />
     <add name="PassportAuthentication" 
	type="System.Web.Security.PassportAuthenticationModule" />
     <add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
     <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
     <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />
     <add name="AnonymousIdentification" 
	type="System.Web.Security.AnonymousIdentificationModule" />
     <add name="Profile" type="System.Web.Profile.ProfileModule" />
</httpModules>   

You can remove the modules you don't need in the web.config like so:

 <httpModules>
  <remove name="PassportAuthentication" />
  <remove name="Profile" />
  <remove name="AnonymousIdentification" />
</httpModules>

For more details, please read this.

Optimizing URL Generation

ASP.NET MVC framework offers the following methods to generate URL:

  1. Html.ActionLink()
  2. Html.RouteLink()
  3. Url.Action()
  4. Url.RouteUrl()

Html.RouteLink() is equivalent to Html.ActionLink():

<%= Html.RouteLink("Click here", new {controller= "Weather", action= "GetWeather"}) %> 
       //will render the following <a href="/Weather/GetWeather">Click here</a>

Similarly, Url.RouteUrl() is equivalent to Url.Action():

<%= Url.RouteUrl(new {controller= "Weather", action= "GetWeather"}) %> 
	will render the following /Weather/GetWeather

However, these methods can have a performance impact on your application. Chad Moran has run performance tests on his blog and he shows improving ASP.NET MVC performance through URL generation.

Useful Links

Here are a few useful links to read more:

Summary

In this article, we examined how to improve the performance of an ASP.NET MVC application by taking advantage of the caching and HTTP compression that are available in .NET Framework. We also examined other open source libraries such as JQuery and combining scripts and other resources to improve the MVC application performance.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here