Rico Suter's blog.
 

A problem we often face when developing ASP.NET MVC applications is that errors in Razor views are only detected at runtime while browsing the site. This is because the Razor views are not compiled until they are requested by the browser.

To avoid this problem, you can prebuild the views by adding the MvcBuildViews tag to your .csproj file:

<?xml version="1.0" encoding="utf-8"?>
<Project ...>
    ...
    <PropertyGroup>
        ...
        <MvcBuildViews>true</MvcBuildViews>
        ...

There are two problems with this approach:

  • The build process takes much longer
  • You cannot change the views when debugging the running application

To avoid these problems and still be able to find view errors, I recommend to enable Razor view compilation only for Release builds. To do so, just add two the following conditional MvcBuildViews tags:

<MvcBuildViews Condition="'$(Configuration)'=='Debug'">false</MvcBuildViews>
<MvcBuildViews Condition="'$(Configuration)'=='Release'">true</MvcBuildViews>

You can also use the Razor view compiler externally, read more in this blog.



Discussion