74

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.

isNaN1247
  • 17,793
  • 12
  • 71
  • 118
  • 6
    I would think this would work exactly the same: http://stackoverflow.com/questions/383192/compile-views-in-asp-net-mvc –  Apr 14 '11 at 17:59
  • Possible duplicate of [Compile Views in ASP.NET MVC](https://stackoverflow.com/questions/383192/compile-views-in-asp-net-mvc) – Jonas Äppelgran Nov 21 '18 at 10:18

3 Answers3

108

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

 <MvcBuildViews>true</MvcBuildViews>
JP.
  • 5,536
  • 7
  • 58
  • 100
  • 4
    Weirdly for me, that didn't work in an existing property group. It only worked when I created a specific property group for it, i.e. ` true ` – Martin Capodici Oct 25 '17 at 21:43
  • What would be more useful is a separate 'Build Views' button that lets me optionally build the project AND the views from time to time - not one or the other all the time. Know any way to do that? (can't see on Google & couldn't find any extensions). – niico Nov 07 '17 at 17:30
  • @niico see @tikall’s response below. You could potentially create a build configuration for this specific task. I’ve never done it so can’t confirm that it works but it certainly sounds like it should/would – JP. Nov 07 '17 at 17:52
  • Thanks I'll try it - an extension that gives you 1 button to build a view would be even nicer but I guess that doesn't exist?! – niico Nov 07 '17 at 19:15
  • I can't get the below working ( tags weren't already there as @Tikall says - I had to add them?!) – niico Nov 07 '17 at 19:20
  • @MartinCapodici check and see if your project file has `false` somewhere else because it might be overriding the new `` element – somethingRandom Feb 24 '22 at 09:54
17

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

Tikall
  • 2,453
  • 25
  • 13
  • I can't get this to work - tags weren't already there, I had to manually add them (if that's relevant). My Views have many errors and a build isn't catching them in either configuration. – niico Nov 07 '17 at 19:20
  • I had the same problem as @niico but discovered that it was the PropertyGroup Condition that did not match my build configuration and platform (condition anyCPU, build x86). – Kobbe Oct 22 '20 at 12:28
0

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!