How to compile cshtml before runtime
Asked Answered
S

4

56

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

Sententious answered 20/11, 2012 at 20:57 Comment(2)
chrisvandesteeg.nl/2010/11/22/…Sporocarp
Possible duplicate of Compile Views in ASP.NET MVCMulatto
S
85

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

Simple answered 20/11, 2012 at 21:0 Comment(6)
any place to set this within the UI?Bellringer
Within the UI of Visual Studio? Not that I'm aware of.Simple
This works, but I noticed it causes a different compiler error, so I had to turn it back off.Bellringer
Are you sure that your compiler error isn't linked to a view compilation problem? Last time I used this feature, when my views didn't compile I got strange compilation error messages...Simple
actually, this is the problem I had: #4725887Bellringer
MvcBuildViews is often mistakenly, as it doesn’t precompile views as you image. See my answer for details (https://mcmap.net/q/1473213/-how-to-compile-cshtml-before-runtime)Tit
I
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>
Isley answered 16/4, 2015 at 1:14 Comment(1)
Error Failed to map the path '/' and Failed to map the path 'temp' what is the reason?Hufford
T
6

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

Tit answered 15/11, 2018 at 12:0 Comment(0)
T
0

Whether if any errors inside a script part in cshtmls can be spotted at build time. Because , when used MvcBuildViews to be true. It compiles alone cshtml but not that script part errors.

Thoraco answered 2/11, 2023 at 9:39 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Astrometry

© 2022 - 2025 — McMap. All rights reserved.