Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C#
Tip/Trick

Optimize ASP.NET MVC Application Performance

Rate me:
Please Sign up or sign in to vote.
4.54/5 (20 votes)
7 Jan 2016CPOL2 min read 35.6K   23   4
In this tip, I explain how we can optimize the ASP.NET MVC application performance.

Introduction

Currently, we are working on very large applications consisting of a number of projects which takes more time to build solution, startup time as well as overall application running time. We have different levels of performance optimization in a web application as below:

  • Build Time
  • App Startup Time
  • Web Application Performance

In this tip, I am sharing my views for these three levels of optimization that I've been applying in my applications.

Web Application Performance

There are multiple things that we can do for optimizing the application performance. Apart from the technical part, we can also configure AppPool as below:

Configure AppPool as Suspended and AlwaysRunning as shown below:

Right click on AppPool -> Advance Settings:

Image 1

It will optimize the time by 20-30%.

Optimize Build Time

Suppose a web application takes approximately 3 minutes to build so each time we change a line of code, we must wait for 3 minutes. Build time should not be >1 minute. If it is, then we should optimize it by applying the following steps:

Step 1: Build Settings

  • Use Parallel Builds
  • Only Build Startup and Dependencies on Run
  • Set Build Verbosity

Image 2

Step 2: Use Razor Generator for PreCompiling MVC Razor Views.

You can refer to the below link for using Razor Generator.

Step a: Go to Tools -> Extension and updates

Search for RazorGenerator -> download it and install

Step b: Download and install razor generator using nuget

  • RazorGenerator.Mvc
  • RazorGenerator.MsBuild

    Or By Console command:

    PM> Install-Package RazorGenerator.Mvc

    PM> Install-Package RazorGenerator.MsBuild

    After installing Razor Generator, it will add the below:

  • RazorGeneratorMvcStart.cs in App_Start folder
  • Below packages:
    • RazorGenerator.MsBuild
    • RazorGenerator.Mvc
    • WebActivatorEx
  • Some code in .csproj

Step c: .csproj changes:

  1. Add the below BeforeBuild target code right after the previously added line to import the RazorGenerator.targets file:
    XML
    <Target Name="BeforeBuild">
        <ItemGroup>
          <Content Remove="Views\**\*.cshtml" />
          <None Include="Views\**\*.cshtml" />
        </ItemGroup>
      </Target>
  2. Add the below properties in .csproj:
    XML
    <PrecompileRazorFiles>true</PrecompileRazorFiles>
    XML
    <MvcBuildViews>false</MvcBuildViews>

    Now Build application, .cs files will be created for cshtml views under /obj/CodeGen/.

Step 3: In Web.config, set optimizeCompilations to true

XML
<Compilation debug="true" targetFramework="4.5" optimizeCompilations="true"/>

Optimize App Startup Time

AppStart time is after rebuilding solution, first page loads time.

From a development point of view, appstart time should be as minimum as possible. In a large application, whenever we change a single line of code and run the app after rebuild, a developer must wait for 2-3 minutes.

We can try the following:

  1. Remove unused DLLs if any.
  2. From the architecture point of view, there should be less number of DLLs.
  3. In dependency injection implementation, Dependency Resolver the lifetime scope should be singleton (ContainerControlledLifetimeManager).

Summary

For easy development, build time and appstart time should be minimum, otherwise developers have to wait unnecessarily for long for a single line code change.

Other Tips

You can also take a look at my other tips:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
Questionpartial view was not found or no view engine supports the searched... Pin
Raphael.UFRPE18-Jun-19 1:06
Raphael.UFRPE18-Jun-19 1:06 
QuestionLooks very similar to another article posted from Kailash Shastri from the same company Pin
delphi.NET6-Jan-16 15:06
delphi.NET6-Jan-16 15:06 
AnswerRe:Optimize ASP.NET MVC Application Performance Pin
Rakhi Shrivastava6-Jan-16 20:39
professionalRakhi Shrivastava6-Jan-16 20:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.