Académique Documents
Professionnel Documents
Culture Documents
Page 1 of 5
,+
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
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.
http://www.codeproject.com/KB/aspnet/How_to_improve_performanc.aspx?display=Print
08/12/2011
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.
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>
http://www.codeproject.com/KB/aspnet/How_to_improve_performanc.aspx?display=Print
08/12/2011
Page 4 of 5
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)
http://www.codeproject.com/KB/aspnet/How_to_improve_performanc.aspx?display=Print
08/12/2011
Page 5 of 5
http://www.codeproject.com/KB/aspnet/How_to_improve_performanc.aspx?display=Print
08/12/2011