54

I believe I read somewhere there is a setting in one of the project files that will allow you to compile the .cshtml files when building your Visual Studio project.

Just getting started with MVC/Razor/Query Mobile, and am getting annoyed with the "Error loading page" when I have an error in a code block inside a .cshtml file

Vivek
  • 11,938
  • 19
  • 92
  • 127
FatFingers
  • 6,847
  • 5
  • 23
  • 20

3 Answers3

84

Set <MvcBuildViews>true</MvcBuildViews> in the <PropertyGroup> element of your .csproj file.

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
16

Besides the true setting, you still need to ensure below setting is active in your csproj:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
karl li
  • 1,316
  • 15
  • 19
5

MvcBuildViews is often mistakenly, as it doesn’t precompile views in the terms you'd like.

MvcBuildViews builds views temporarily and gives build results back to building process to show possible build error in VS errors window.

For achieving a real precompilation of views for production release, you have to properly set specific values in the Publish Web App configuration window of your project.

See here for the main article about how to do it via Visual Studio and here for going deeper on how to do it via msBuild and Azure.

UPDATE

In a nutshell, with Visual Studio you have to configure the Publish window as:

Main publish window

and

Advanced configuration window

Gianpiero
  • 3,349
  • 1
  • 29
  • 42