MVCBuildViews无法正常工作

Sco*_*ott 49 c# msbuild asp.net-mvc visual-studio-2010

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

<MvcBuildViews>true</MvcBuildViews>
Run Code Online (Sandbox Code Playgroud)

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

在应用程序级别之外使用注册为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>
Run Code Online (Sandbox Code Playgroud)

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

Nic*_*eri 54

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

请参阅Joe Cartano的答案,了解更持久的解决方案.

  • 我试过这个,但唉它对我不起作用.为了成功,我将尝试下面的其他解决方案. (2认同)

Chr*_*nes 37

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

另一个解决方法是在调用<AspNetCompiler>之前核对发布输出.打开你的.csproj并更改:

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

对此:

<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>
Run Code Online (Sandbox Code Playgroud)

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

更新:

更好的是,基于/sf/answers/3400759771/,您可以完全排除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>
Run Code Online (Sandbox Code Playgroud)


Joe*_*ano 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.

  • 是的,谢谢.我把它添加到我的.proj文件:`<BaseIntermediateOutputPath> ..\bin </ BaseIntermediateOutputPath>`所以现在obj文件夹处于解决方案级别,构建问题就消失了! (7认同)

arn*_*rni 5

这对我有用。或者,您可以通过配置指定条件。

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