Saturday, 13 September 2014

Tips and Tricks to improve MVC Application Performance

In this post we will cover a few tips and tricks to improve ASP.NET MVC Application Performance:

(1) Run in Release mode

Make sure your production application always runs in release mode in the web.config
<compilation debug="false"></compilation>
Or change this in the machine.config on the production servers
          <deployment retail="true"></deployment>


(2) Only use the View Engines that you require

protected void Application_Start()
    ViewEngines.Engines.Add(new RazorViewEngine());


(3) Use the CachedDataAnnotationsModelMetadataProvider

ModelMetadataProviders.Current = new CachedDataAnnotationsModelMetadataProvider();

(4) Avoid passing null models to views

Because a NullReferenceException will be thrown when the expression gets evaluated, which .NET then has to handle gracefully.
// BAD
public ActionResult Profile()
    return View();

public ActionResult Profile()
    return View(new Profile());


(5) Use OutputCacheAttribute when appropriate

For content that does not change often, use the OutputCacheAttribute to save unnecessary and action executions.
<urlcompression dodynamiccompression="true" dostaticcompression="true" dynamiccompressionbeforecache="true"></urlcompression>



(6) Remove unused HTTP Modules

If you run into any problems after removing them, try adding them back in.
      <remove name="WindowsAuthentication"></remove>
      <remove name="PassportAuthentication"></remove>
      <remove name="Profile"></remove>
      <remove name="AnonymousIdentification"></remove>



(7) Flush your HTML as soon as it is generated

<pages buffer="true" enableviewstate="false"></pages>



(8) Turn off Tracing

          <trace enabled="false"></trace>



(9) Remove HTTP Headers

This is more of a security thing:
    <httpruntime enableversionheader="false"></httpruntime>

          <remove name="X-Powered-By"></remove>



(10) Uninstall the URL Rewrite module if not required

This saves CPU cycles used to check the server variable for each request.
Go to "Add or Remove Programs" and find "Microsoft URL Rewrite Module" and select uninstall. 

(11) Minify Js, CSS, Gzip Compress
Minification involves removing unnecessary characters from code to reduce its size. This reduction in size greatly improves load times. There are some great tools out there to use - JSMin, YUI Compressor, etc.

(12) Use a Content Delivery Network
Serving your content from multiple worldwide servers allows much faster loads time on the user's side. These servers can be used to cache and deliver all types of content. On this site I am using the Google CDN to load my jQuery API. To do this, you simply need to replace your local version of jQuery with the one on the CDN:
<script type="text/javascript" src=""></script>

Google has support for loads of different javascript libraries. Microsoft also offers Microsoft AJAX CDN, which service you choose to go with is all up to you.

1 comment:

  1. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.
    WordPress website development Chennai