19

My Visual Studio Ultimate 2013 Update 4 does not compile ASP.NET MVC 5 views.

Compilation errors are spotted on views sporadically, although compilation is always successful. Intellisense is also on and off on the views. I would say it was working significantly better in VS2012 (I did not much MVC on that version).

I have tried to add <MvcBuildViews>true</MvcBuildViews> to the .csproj file, thing that uses to work in VS2010, but it is not working anymore.

Any idea what could be the problem?

UPDATE: I am looking for the way of seeing the errors on the view, as it was happening in previous versions of VS.

mason
  • 31,774
  • 10
  • 77
  • 121
vtortola
  • 34,709
  • 29
  • 161
  • 263
  • Do you use Resharper? – Ofiris Feb 03 '15 at 19:54
  • 1
    RazorGenerator is much better for compiling views. I'd consider switching to it: https://visualstudiogallery.msdn.microsoft.com/1f6ec6ff-e89b-4c47-8e79-d2d68df894ec and NUGET: https://www.nuget.org/packages/RazorGenerator.Mvc/ – Mathew Thompson Feb 05 '15 at 14:31

2 Answers2

32

Right click your web project in the Solution Explorer. Click Unload Project. Right click the project and click Edit <projname>.csproj.

Make sure you have this element (add it if it doesn't exist).

<Project>
  <PropertyGroup>
    <MvcBuildViews>true</MvcBuildViews>
  </PropertyGroup>
</Project>

Scroll down to the bottom. You should see some comment "To modify your build process, add your task inside one of the targets below and uncomment it." Below that, add this markup:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <ItemGroup>
      <ExtraWebConfigs Include="$(BaseIntermediateOutputPath)\**\web.config" />
      <ExtraPackageTmp Include="$([System.IO.Directory]::GetDirectories(&quot;$(BaseIntermediateOutputPath)&quot;, &quot;PackageTmp&quot;, System.IO.SearchOption.AllDirectories))" />
    </ItemGroup>
    <Delete Files="@(ExtraWebConfigs)" />
    <RemoveDir Directories="@(ExtraPackageTmp)" />
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
  </Target>

(the above code should have a parent of the root <Project> node in case you don't see the comment I mentioned)

Close the .csproj file, then right click your project in the Solution Explorer and click Reload Project.

This will add your views to the compilation step, and will stop your build if errors are found. I found this to be a good thing, because without it I sometimes don't notice errors in the Error List until I've deployed my site and then manually hit them. Be warned, it will add some time to your build step, significantly slowing it down. Depending on what you're trying to achieve, you may want to selectively enable/disable it to achieve a rapid build -> test workflow.

Inspiration for this answer was taken from Chris Hyne's answer to MVCBuildViews not working correctly and Can Razor views be compiled?.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
  • This just compiles the view after build, and shows the error on the generated C# file. But what I need is the compilation errors in the view. – vtortola Feb 05 '15 at 14:28
  • @vtortola I'm not sure I understand what you're after then. With this configuration, it will halt your build, show the list of compilation errors, then you can double click them to go to the appropriate view and deal with them. If that's not what you're after, can you please explain better? – mason Feb 05 '15 at 14:31
  • wait it works now. I got an error about an unexistent model, there are two errors, one bring me to the view and the other just to a generated C# file. Thanks! – vtortola Feb 05 '15 at 14:46
  • Does this still work in VS2015 with MVC5? It doesn't seem to work for me. My workaround was to compile views on the build server by enabling precompile during publish but that seems to prevent EmbeddedResourceVirtualPathProvider from working, which is required for this project. I really want some error checking for cshtml but it currently doesn't seem possible. – Russell Horwood Aug 15 '16 at 09:12
15

Does your project include the target and hook into after build event? Try msbuild WebApplication1.csproj /t:MvcBuildViews from command line, it'll check that you have both default property set and target defined.

<PropertyGroup>
    <MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

Try creating a blank MVC project from within the VS2013U4 and compare the csproj to yours.

Ilya Kozhevnikov
  • 10,242
  • 4
  • 40
  • 70
  • MvcBuildViews set to false in VS2013 MVC5 app. Thanks for the info. Switched on and now I can see where to look - granted it is the generated MVC files but the name indicates which razor view. Was a nightmare having to go through all views after a namespace rename. – Andez May 23 '15 at 09:34