Compile razor views at build time

When running MVC locally in development, MVC Views are actually compiled at runtime, when you first visit a view, the cshtml view is being compiled to a CS file and embedded to your web assembly before it is bind to your model and returned to the client.

One thing I don’t like about this approach is that it can leave some compilations error in case you refactor a model or mvc helper. That error is not going to be detected until you precompile your views in your build / publish process.

Another thing is that add up to the start time of your application in development, as each view needs to be recompiled every time you restart your app.

Would be nice if we could compile views during the build process. And there is actually a visual studio extension to do that.

To install the extension, go to tools -> Extensions and updates

Now search for Razor generator


Once installed open the Nuget package manager console and run


This will add a custom tool flag to all your cshtml files that will run the razor compiler for your views at build time.


It’s as simple as that.

If for whatever reason you want to rebuild all your cshtml views, you can run the following in the package management console



You can find the project on github


  1. Troubleshoot startup performance of MVC Applications | David Bérubé's blog - […] Compile razor views at build time […]

Submit a Comment

Your email address will not be published. Required fields are marked *

Share This