Compilation errors in Razor Views
After a some searching in the documentation of Visual Studio, MSBUILD and some other things; and some other blogs and Stack Overflow. I was able to find a way to be able to compile Razor views, and after compilation Visual Studio populated the Errors list with errors in views.
One thing to keep in mind is, the compilation of views does take a lot more time, then the rest of the solution. While testing this out, our solutions build time was increased from < 30 secs to ca 2.5 minutes. Which almost a 5 times increase in compilation time. I figured, I can use this only when it is really needed. Like after a merge. All I have to do is change one boolean in .csproj file with Razor views.
First of all you need to add this in the first <PropertyGroup />
.
<PropertyGroup>
<MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>
I have it just under <TargetFrameworkVersion />
tag. Then it is good to add a condition, so that you can toggle <MvcBuildViews />
tag, so that under development you don't have to wait for views to compile.
<Target Name="BuildViews" Condition="'$(MvcBuildViews)'=='true'" AfterTargets="Build">
<Message Importance="normal" Text="Precompiling views" />
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
In some cases it is also a good idea to add this just under <OutputPath />
tag:
<IntermediateOutputPath>..\Whatever\obj\</IntermediateOutputPath>