Click here to Skip to main content
15,397,498 members
Articles / Web Development / IIS
Posted 30 Aug 2009


88 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.90/5 (14 votes)
5 Sep 2009CPOL4 min read
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


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.

     <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" />

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

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

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:


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.


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


About the Author

Farooq Kaiser
Software Developer (Senior)
Canada Canada
No Biography provided

Comments and Discussions

QuestionChad Moran's link is dead, you may update with the one I just have found Pin
leo_ullas27-Jan-16 16:37
Memberleo_ullas27-Jan-16 16:37 
GeneralTips and Tricks on how to improve MVC Application Performance Pin
LuciferUk20-Apr-13 23:46
MemberLuciferUk20-Apr-13 23:46 
Questionexcellent article Pin
Member 789779517-Apr-12 12:40
MemberMember 789779517-Apr-12 12:40 
Generaldistributed caching. Pin
Wes Malik9-Jan-11 18:04
MemberWes Malik9-Jan-11 18:04 
Generalok article Pin
Donsw2-Jun-10 11:07
MemberDonsw2-Jun-10 11:07 
OK but like the others said concentraet more on MVC. keep the aricles coming.
My Recent Article : CDC - Change Data Capture

GeneralMy vote of 2 Pin
Jeremy McPeak2-Sep-09 9:32
MemberJeremy McPeak2-Sep-09 9:32 
GeneralGood try but.... Pin
Shivprasad koirala30-Aug-09 16:40
MemberShivprasad koirala30-Aug-09 16:40 

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.