MvcBuildViews setting depending on build configuration


By default, in a MVC web application, MVC Razor views (.cshtml files) are not compiled at build time, so any errors in these parts of the application will only occure during run time. When you are editing a .cshtml file your Error window will show the errors in that specific file, but these errors will not break the build of the application. You could argue that this is acceptable during development time, but this is a recipe for disaster if you are compiling for deployment to test/production environments.

The MvcBuildViews project setting is the solution if you want to compile the views during build time and get errors before run time. However, compiling the .cshtml files will slow down your build and if your project depends heavily on razor views this can become a real productivity issue.

Use build configurations to set MvcBuildViews

To have quick builds during development time and still benefit from .cshtml compilation when building for deployment you can use Debug/Release build configurations.

In Debug mode, do not compile the .cshtml to have a shorter build time. Errors in .cshtml will occure run time, but as you are still developing there could just as well be errors in the html, js or css and those errors will only be detected by testing your work in a browser anyway. If you suspect a big amount of .cshtml problems after running into a runtime .cshtml error  you can always set the MvcBuildViews setting temporarily to true in order to fix the .cshtml errors.

In Release mode, compile the .cshtmls view to detect .cshtml errors while building.

The way to do this is to edit the Visual Studio project file (.csproj). To edit the .csproj right click the project in the Visual Studio solution explorer and choose “Unload project”. After unloading the project right click the project again and choose “Edit <projectname>.csproj”. After the project file opens you see something like:

The <PropertyGroup> element is repeated for every build configuration, so we can add settings depending on build configuration.

In the <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> section set the MvcBuildViews element to false: <MvcBuildViews>false</MvcBuildViews>.

In the <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> section set the MvcBuildViews element to true: <MvcBuildViews>true</MvcBuildViews>.

Now save the .csproj file and right click the project in the Visual Studio solution explorer again and choose “Reload Project” to finish up.

Finally test building your solution in Debug and Release mode with errors in a .cshtml file to make sure everything works!


Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>