How to pre-compile razor view in ASP.NET MVC?


By default, razor views are not compiled with projects. They compile at runtime.

Problem:

Razor view will not give you a compile time error if you have used a wrong property name or property name changes. It throws a runtime exception.

Solution:

You can turn on compilation in your project file.

Open the .csproj file in notepad. Find <mvcbuildviews> in the .csproj file. Change <MvcBuildViews>false</MvcBuildViews> to <MvcBuildViews>true</MvcBuildViews>.

Now, all the razor views will be precompiled with other parts of the MVC project.