MVCBuildViews不能正常工作

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

<MvcBuildViews>true</MvcBuildViews> 

这应该会导致我的观点在编译期间得到遵守,如果我的观点被破坏,则强制执行构build错误。 这是我做的唯一的改变,但是,当我尝试构build应用程序时,出现以下错误:

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

如果我更改为false,项目编译并成功运行,

以下是在csproj文件中configuration的构build任务(这些都不是手动编辑的,它们是由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 2010configuration正确,以validation我的意见,在编译时?

前几天我有这个问题,我通过删除obj / Debug文件夹来修复它。

编辑:请参阅Joe Cartano的答案,以获得更持久的解决scheme。

在obj文件夹中存在Web项目输出(模板化web.config或临时发布文件)时,会发生此问题。 使用的ASP.NET编译器不够聪明,忽略obj文件夹中的东西,所以它会抛出错误。

另一个解决方法是在调用<AspNetCompiler>之前调用发布输出。 打开你的.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文件夹。

当你得到这个错误,你的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正在由你正在运行的一些工具生成。

这是为我工作。 或者,您可以使用configuration指定条件。

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

尝试改变这个AspNetCompiler行:

 <AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />