VS2013 does not compile ASP.NET MVC5 views

2020-01-29 06:17发布

问题:

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.

回答1:

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.



回答2:

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?.