How to compile cshtml before runtime

asp.net MvcJquery MobileRazor

asp.net Mvc Problem Overview


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

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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

Solution 2 - asp.net Mvc

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>

Solution 3 - asp.net Mvc

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, and here for going deeper on how to do it via msBuild and Azure.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFatFingersView Question on Stackoverflow
Solution 1 - asp.net MvcLevi BotelhoView Answer on Stackoverflow
Solution 2 - asp.net Mvckarl liView Answer on Stackoverflow
Solution 3 - asp.net MvcGianpieroView Answer on Stackoverflow