MVCBuildViews无法正常工作

时间:2011-01-18 14:44:12

标签: c# asp.net-mvc visual-studio-2010 msbuild

所以我在MVC 3 RTM应用程序上编辑了我的csproj文件,以设置以下属性:

<MvcBuildViews>true</MvcBuildViews>

这会导致我的视​​图在构建期间被编译,并在我的视图被破坏时强制构建错误。这是我做的唯一更改,但是,当我尝试构建应用程序时,出现以下错误:

  

在应用程序级别之外使用注册为allowDefinition ='MachineToApplication'的部分是错误的。此错误可能是由于虚拟目录未在IIS中配置为应用程序。

如果我改回false,项目将成功编译并运行

以下是csproj文件中配置的构建任务(这些任务从未手动编辑,它们是由Visual Studio 2010添加的)

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

我在这里遗漏了什么吗?如何正确配置MVC 3 / Visual Studio 2010以在构建时验证我的视图?

6 个答案:

答案 0 :(得分:53)

几天前我遇到了这个问题,我通过删除obj / Debug文件夹修复了它。清洁项目也有效。不过,我不知道问题的原因。

有关更长久的解决方案,请参阅Joe Cartano's answer

答案 1 :(得分:37)

当obj文件夹中存在Web项目输出(模板化web.config或临时发布文件)时,会发生此问题。使用的ASP.NET编译器不够智能,无法忽略obj文件夹中的内容,因此会抛出错误。

另一个解决方法是在调用&lt; AspNetCompiler&gt;之前核对发布输出。打开你的.csproj并改变它:

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

到此:

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

这将删除\ obj下的所有web.configs,以及\ obj下的所有PackageTmp文件夹。

<强>更新

更好的是,基于https://stackoverflow.com/a/48582282/8037,您可以完全排除obj文件夹。显然<AspNetCompiler />任务没有exclude参数,但是如果你切换到直接调用aspnet_compiler .exe,你可以像这样排除obj:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <Exec Command="$(MSBuildFrameworkToolsPath)aspnet_compiler.exe -v temp -p $(WebProjectOutputDir) -x $(BaseIntermediateOutputPath)"/>
</Target>

答案 2 :(得分:22)

当您收到此错误时,您的obj文件夹中是否有另一个web.config文件?如果您正在使用MSDeploy,这可能有所帮助:http://blogs.msdn.com/b/webdevtools/archive/2010/05/14/the-aspnet-compiler-build-task-in-visual-studio-2010-asp-net-mvc-2-projects.aspx,如果没有,可能正在运行的某个工具生成另一个web.config。

答案 3 :(得分:3)

这对我有用。 (可选)您可以使用配置指定条件。

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
<Target Name="AfterBuild" Condition="'$(Configuration)'!='Debug'">
  <RemoveDir Directories="$(BaseIntermediateOutputPath)" />
</Target>

答案 4 :(得分:1)

一个简单的解决方案,是根据其他答案在这里编译的

您可以像这样简单地删除整个/obj文件夹:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" /> <!--add this line-->
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

答案 5 :(得分:0)

以下MSDN博客很好地解释了即使MvcBuildViews设置为“ true”,此期的编译时视图检查问题:

https://blogs.msdn.microsoft.com/jimlamb/2010/04/20/turn-on-compile-time-view-checking-for-asp-net-mvc-projects-in-tfs-build-2010/

您可以通过直接编辑 .csproj 文件来进行修复:

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

 <Target Name="BuildViews" Condition="'$(MvcBuildViews)'=='true'" AfterTargets="Build">
   <Message Importance="normal" Text="Precompiling views" />
   <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
 </Target>