Click here to Skip to main content
15,867,568 members
Articles / Web Development / IIS

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 110.5K   88   7
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.

C#
[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:

C#
[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:

C#
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.

ASP.NET
<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.

ASP.NET
 <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:

ASP.NET
 <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():

ASP.NET
<%= 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():

ASP.NET
<%= 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) http://www.Fairnet.com
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

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
leo_ullas27-Jan-16 16:37 
GeneralTips and Tricks on how to improve MVC Application Performance Pin
LuciferUk20-Apr-13 23:46
LuciferUk20-Apr-13 23:46 
Questionexcellent article Pin
Member 789779517-Apr-12 12:40
Member 789779517-Apr-12 12:40 
Generaldistributed caching. Pin
Wes Malik9-Jan-11 18:04
Wes Malik9-Jan-11 18:04 
Generalok article Pin
Donsw2-Jun-10 11:07
Donsw2-Jun-10 11:07 
GeneralMy vote of 2 Pin
Jeremy McPeak2-Sep-09 9:32
Jeremy McPeak2-Sep-09 9:32 
GeneralGood try but.... Pin
Shivprasad koirala30-Aug-09 16:40
Shivprasad koirala30-Aug-09 16:40 
Its a good try.When i clicked on this article my impression was you will revolve around how performance can be improved in entities like model , view and controller or as a group.

Many point over here are not really related to MVC performance improvement like "Deploying production code in release mode". I mean they are obvious. If you improve this article and pour in more bit around the three things i.e. M , V and C - the article would look more relevant.

I do understand how much pain a author takes to write a article. Sp please do not take my thoughts on a negative side.

Visit my 500 videos on WCF,WPF,WWF,Silverlight,UML design patters @ http://www.questpond.com

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.