13

I'm new to asp.net mvc. I see nasty little red underlines on my .cshtml page in some razor code with errors in the error list, but the project still builds. Can I fail the project build somehow if these errors are about?

As an aside, if I rename a variable in a c# class referred to in a razor page, why doesn't the refactor tool extend its reach to the cshtml file?

Isaac Bolinger
  • 7,328
  • 11
  • 52
  • 90

2 Answers2

31

You need to set in your project to compile views during build.

Set in your MVC project:

<MvcBuildViews>true</MvcBuildViews>

All you have to do is unload or close the mvc project in Visual Studio, edit the .cspoj file (in VS or notepad or your favourite editor), locate <MvcBuildViews> and change the value to true.

Romias
  • 13,783
  • 7
  • 56
  • 85
4

It could be smart using the MvcBuildViews setting only for Release builds, assuming your build process uses a Release build before you deploy. This will remove the extra wait to compile the views when you’re developing and debugging, but still keeps a check in place before you deploy. Add the MvcBuildViews node to the PropertyGroup node for Release builds only. It will look something like this:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
    <MvcBuildViews>true</MvcBuildViews>
</PropertyGroup> 

http://blog.falafel.com/get-compile-time-view-errors-in-asp-net-mvc/

  • I had to do exactly this for a different reason, actually. The builder in deploy mode was throwing nonexistent errors from the asp compiler. So, I use MvcBuildViews only on Release mode. This is checked every time I commit anyway. – Isaac Bolinger Jan 25 '16 at 20:04
  • There's a discussion of that here http://stackoverflow.com/questions/28665470/why-wont-my-msbuild-deploy-work-after-i-check-precompile-during-publishing-on – Isaac Bolinger Jan 25 '16 at 20:21