
In this post we will cover a few tips and tricks to improve ASP.NET MVC Application Performance.
Run in Release mode
Make sure
your production application always runs in release mode in the web.config
| 
1 | <compilationdebug="false"></compilation> | 
or change
this in the machine.config on the production servers
| 
1 
2 
3 
4 
5 | <configuration>    <system.web>          <deploymentretail="true"></deployment>    </system.web></configuration> | 
Only use the View Engines that you require
| 
1 
2 
3 
4 
5 | protectedvoidApplication_Start()
  {
      ViewEngines.Engines.Clear();
      ViewEngines.Engines.Add(newRazorViewEngine());
  } | 
Use the
CachedDataAnnotationsModelMetadataProvider
| 
1 | ModelMetadataProviders.Current
  = newCachedDataAnnotationsModelMetadataProvider(); | 
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();
}
// GOOD
public ActionResult Profile()
{
    return View(new Profile());
}
Use OutputCacheAttribute when appropriate
For
content that does not change often, use the OutputCacheAttribute to save
unnecessary and action executions.
| 
1 
2 
3 
4 
5 | [OutputCache(VaryByParam
  = "none", Duration = 3600)]publicActionResult
  Categories() {
      returnView(newCategories());
  } | 
Use HTTP Compression
| 
1 
2 
3 | <system.webserver>
  <urlcompressiondodynamiccompression="true"dostaticcompression="true"dynamiccompressionbeforecache="true"></urlcompression></system.webserver> | 
Remove unused HTTP Modules
If you run into any problems after removing
them, try adding them back in.
| 
1 
2 
3 
4 
5 
6 | <httpmodules>      <removename="WindowsAuthentication"></remove>      <removename="PassportAuthentication"></remove>      <removename="Profile"></remove>      <removename="AnonymousIdentification"></remove></httpmodules> | 
Flush your HTML as soon as it is generated
| 
1 | <pagesbuffer="true"enableviewstate="false"></pages> | 
Turn off Tracing
| 
1 
2 
3 
4 
5 | <configuration>     <system.web>          <traceenabled="false"></trace>     </system.web></configuration> | 
Remove HTTP Headers
This is
more of a security thing
| 
1 
2 
3 | <system.web>    <httpruntimeenableversionheader="false"></httpruntime></system.web> | 
| 
1 
2 
3 
4 
5 | <httpprotocol> <customheaders>  <removename="X-Powered-By"></remove> </customheaders></httpprotocol> | 
Uninstall the URL Rewrite module if not
required
This saves
CPU cycles used to check the server variable for each request.
| 
1 | Go
  to "Add or Remove Programs" and find "Microsoft URL Rewrite
  Module" and select uninstall.  | 
 
0 Comments