Vous êtes sur la page 1sur 5

How To Improve the Performance of ASP.

NET MVC Web Application - CodeProject

Page 1 of 5

Web Development ASP.NET General

How To I m prove the Perform ance of ASP.NET MVC W eb Application


By Farooq Kaiser | 5 Sep 2009

Licence First Posted Views Bookmarked

CPOL 30 Aug 2009 21,788 60 times

IIS6 VS2005 IIS7 .NET3.5 VS2008 C# ASP.NET Architect Dev Advanced

,+

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
4.25 (12 votes)

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. 2. 3. 4. 5. 6. 7. Implementing Caching Implementing HTTP Compression Implementing JQuery UI library with Google Hosted Ajax Libraries By combining scripts and other resources Deploying production code in release mode Removing default HTTP modules in ASP.NET 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) {

http://www.codeproject.com/KB/aspnet/How_to_improve_performanc.aspx?display=Print

08/12/2011

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

Page 2 of 5

} }

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

http://www.codeproject.com/KB/aspnet/How_to_improve_performanc.aspx?display=Print

08/12/2011

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

Page 3 of 5

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. 2. 3. 4.

Html.ActionLink() Html.RouteLink() Url.Action() Url.RouteUrl()

http://www.codeproject.com/KB/aspnet/How_to_improve_performanc.aspx?display=Print

08/12/2011

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

Page 4 of 5

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: Best Practices for fast websites Exceptional Performance Nice JavaScript libraries to help reduce the number of requests CSS Sprites: Image Slicing's Kiss of Death

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)

About the Author


Farooq Kaiser 12+ years of complete software development life cycle experience for web based applications and multi-tier client-server desktop, primarily using LINQ, WCF, WWF, C#, ASP.NET, XML, XSLT, AJAX, Winforms,Visual Basic, JavaScript, JQuery, Google APIs, C++, VB.NET, C, ATL/COM, Open XML. Extensively involved in the requirement analysis, feasibility study, conceptualization, planning, architecture/design, configuration, development, quality assurance, implementation and release of the software products.

Software Developer (Senior) http://www.Fairnet.com Canada Member

http://www.codeproject.com/KB/aspnet/How_to_improve_performanc.aspx?display=Print

08/12/2011

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

Page 5 of 5

Comments and Discussions


4 messages have been posted for this article Visit http://www.codeproject.com/KB/aspnet/How_to_improve_performanc.aspx to post and view comments on this article, or click here to get a print view with messages.
Permalink | Advertise | Privacy | Mobile Web04 | 2.5.111206.1 | Last Updated 5 Sep 2009 Article Copyright 2009 by Farooq Kaiser Everything else Copyright CodeProject, 1999-2011 Terms of Use

http://www.codeproject.com/KB/aspnet/How_to_improve_performanc.aspx?display=Print

08/12/2011

Vous aimerez peut-être aussi