Check for razor errors during build

C#Visual StudioRazor

C# Problem Overview


Is there a way for Visual Studio (I'm using 2010) to find errors within razor views during builds, in the same way as other code in a C# project would?

It's just a pain that you can check any errors in your code and think that everything's fine, but it appears that you can't be sure about views unless you go through each one.

BTW I obviously don't code in my views - I'm just talking about HTML or URL extension methods for example.

C# Solutions


Solution 1 - C#

Try setting MVCBuildViews to true in your project file (i.e. edit your csproj file)

 <MvcBuildViews>true</MvcBuildViews>

Solution 2 - C#

Building views takes a while and the extra 10+ seconds to do a debug build can get annoying fast, so I usually only set the MvcBuildViews to true on release type build configurations. That way, if you have a build server it will catch the error for you, or you can manually run a release build every now and then to check your views.

I don't think order matters for PropertyGroup elements, but for a more complete example i included elements above and below the MvcBuildViews element.

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    ...
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    <MvcBuildViews>false</MvcBuildViews>
    <UseIISExpress>false</UseIISExpress>
    ...
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    <ErrorReport>prompt</ErrorReport>
    <MvcBuildViews>true</MvcBuildViews>
    <WarningLevel>4</WarningLevel>
    ...
</PropertyGroup>

The MvcBuildViews element in the top PropertyGroup was added by VS on project creation, the build configuration specific one (bottom PropertyGroup) i added manually

Solution 3 - C#

Try add in mode edit of project the following assembly: System.core, according to the code:

...
<Reference Include="System.Core, Version=4.0.0.0" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.WebRequest" />
...

Sometimes this assembler not loader correctly, In My case, it worked!

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
QuestionisNaN1247View Question on Stackoverflow
Solution 1 - C#JP.View Answer on Stackoverflow
Solution 2 - C#TikallView Answer on Stackoverflow
Solution 3 - C#Fábio Rodrigues FonsecaView Answer on Stackoverflow