In a traditional ASP.NET MVC application, Razor views are compiled by the server the first time they’re requested.
Further calls will result in the same compilation results being served again. These compiled view results are then available until IIS is restarted, the application pool is recycled or the application is shut down.
When that happens and the application is restarted, the next client to request a specific view will bear the compilation cost once more. In a high volume application, the application should be kept online as more requests arrive and compilation will thus happen infrequently.
In certain scenarios, for example when the application is accessed infrequently or when view compilation simply takes to much time, precompilation is a viable solution.
It allows us to precompile the views ahead of time which precludes IIS from needing to compile them at runtime.
You can precompile the views when you are building the application in Visual Studio, on the build server when doing a build or after the application is built but before it is deployed. This last one was possible with Web Forms but I have not investigated this avenue with MVC.
Let’s look at the pros and cons of the first two methods.
Precompiling on the developer’s machine
- Find potential view errors at compile time rather than runtime (see example below).
- Speed up the application for developers by preventing view compilation when debugging.
- Allows us to share views between ASP.NET MVC projects in the same solution.
- Cannot modify views when debugging or when the application is running. It needs to be stopped, compiled and started again for changes to take effect.
- Compiled files will appear in merge conflicts. Note that you can simply ignore the conflicts and rebuild to solution to generate the files again.
- Generated files will be checked in to source control, polluting the repository. On the plus side this allows other developers to reuse the precompilation results.
- Need to install a Visual Studio extension and a NuGet package.
Precompiling on the build server
- No change in workflow for developers. They can continue modifying views at runtime, they do not need to install any tools.
- No precompilation files in source control.
- Will not find view errors at compile time.
- Will not speed up the application on the developer’s machines.
- Requires a build server and build process (for those who aren’t using one).
In my last ASP.NET MVC project, I chose the first option for many reasons but primarily because I needed to share views between projects. To do this you need to have the views compiled and then copy the compiled views in a folder in the destination project. This process would require it’s own blog post so I will leave it for a possible future post.
Here’s an example of a precompilation error I mentionned earlier:
@model TestModel @Model.First @Model.Seond <h2>@ViewBag.Title.</h2> <h3>@ViewBag.Message</h3>
In our model we have two properties, First and Second. In the previous example an error has been introduced where the property is misspelled Seond. While this error will show up visually with a red squiggly line when you inspect the file, the application will compile just fine but throw an exception when you try to access this view.
When working on a large project with dozens of views, it’s easy to miss these errors.
Activating precompilation in Visual Studio
To activate precompilation you need to install the following NuGet package in each project which will contain precompiled views: RazorGenerator.Mvc.
You also need to install the following extension in Visual Studio: Razor Generator.
Finally you need to change the build tool for the views that need to be precompiled, often case this means all views, by setting the custom tool to RazorGenerator.
You can visit the RazorGenerator GitHub page for more information.