How do you improve your ASP.NET MVC application performance?
How do you improve your ASP.NET MVC application performance?
A compiled list of possible sources of improvement are below:
General
Caching
CompiledQuery.Compile()
recursively avoiding
recompilation of your query
expressionsOutputCacheAttribute
to save unnecessary and action
executionsActionResult methods if necessaryRouteName to organize your routes and then use it to generate
your links, and try not to use the expression tree based ActionLink method. PartialViews, avoid render it xxxx times: if you
end up calling the same partial 300 times in the same view, probably there is something
wrong with that. Explanation And BenchmarksRouting
Use Url.RouteUrl("User", new { username = "joeuser" }) to specify routes. ASP.NET MVC Perfomance by Rudi Benkovic
Cache route resolving using this helper UrlHelperCached ASP.NET MVC Perfomance by Rudi Benkovic
Security
DAL
Load balancing
Utilize reverse proxies, to spread the client load across your app instance. (Stack Overflow uses HAProxy (MSDN).
Use Asynchronous Controllers to implement actions that depend on external resources processing.
Client side
Global configuration
If you use Razor, add the following code in your global.asax.cs, by default, Asp.Net MVC renders with an aspx engine and a razor engine. This only uses the RazorViewEngine.
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
Add gzip (HTTP compression) and static cache (images, css, ...) in your web.config
<system.webServer>
<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/>
</system.webServer>
<pages buffer="true" enableViewState="false">The basic suggestion is to follow REST principles and the following points ties some of these principals to the ASP.NET MVC framework:
Code Climber and this blog entry provide detailed ways of increasing application's performance.
Compiled query will increase performance of your application, but it has nothing in common with ASP.NET MVC. It will speed up every db application, so it is not really about MVC.
Not an earth-shattering optimization, but I thought I'd throw this out there - Use CDN's for jQuery, etc..
Quote from ScottGu himself: The Microsoft Ajax CDN enables you to significantly improve the performance of ASP.NET Web Forms and ASP.NET MVC applications that use ASP.NET AJAX or jQuery. The service is available for free, does not require any registration, and can be used for both commercial and non-commercial purposes.
We even use the CDN for our webparts in Moss that use jQuery.
When accessing data via LINQ rely on IQueryable ...
Why use AsQueryable() instead of List()?
... and leverge a good Repository pattern:
Loading Subrecords in the Repository Pattern
This will optimize data access to ensure only the data needed is loaded and when only it is needed.
Also if you use NHibernate you can turn on and setup second level cache for queries and add to queries scope and timeout. And there is kick ass profiler for EF, L2S and NHibernate - http://hibernatingrhinos.com/products/UberProf. It will help to tune your queries.
In addition to all the great information on optimising your application on the server side I'd say you should take a look at YSlow. It's a superb resource for improving site performance on the client side.
This applies to all sites, not just ASP.NET MVC.
One super easy thing to do is to think asynchronously when accessing the data you want for the page. Whether reading from a web service, file, data base or something else, use the async model as much as possible. While it won't necessarily help any one page be faster it will help your server perform better overall.
If you are running your ASP.NET MVC application on Microsoft Azure (IaaS or PaaS), then do the following at least at before the first deployment.
Use the latest version of Task Parallel Library (TPL), according to .Net version. Have to choose the correct modules of TPL for different purposes.
I did all the answers above and it just didn't solve my problem.
Finally, I solved my slow site loading problem with setting PrecompileBeforePublish in Publish Profile to true. If you want to use msbuild you can use this argument:
/p:PrecompileBeforePublish=true
It really help a lot. Now my MVC ASP.NET loads 10 times faster.