当我将 csproj 中的 MvcBuildViews 属性设置为 true 时,我遇到了同样的错误。经过大量研究和反复试验,我了解到问题是因为我们的站点在站点结构中有 .java 文件。这些 java 文件不是解决方案的一部分,只是松散的文件。Aspnetcompiler 任务从项目根目录运行,因此它会发现各种问题,例如重复的 web.configs 和 *.java 文件。
为了解决这个问题,我在尝试调试的 MVC 项目文件中创建了以下目标:
<Target Name="MvcBuildViews" AfterTargets="Build" Condition="'$(MvcBuildViews)'=='true'">
<!-- This task performs compilation of the CSHTML files in the web structure
and will generate compiler errors if there are issues in the views, such as missing
resource strings, broken class locations, etc.
Due to an issue with the AspNetCompiler task identifing .java files as candidates for
compilation, we will temporarily rename all of the java files in the project to .xyz
so they are skipped by aspnet compiler. Then we rename them back.
Extra web.configs also cause an error, so those are temporarily moved. -->
<CreateItem Include="$(ProjectDir)**\*.java">
<Output TaskParameter="Include" ItemName="JavaFolderA"/>
</CreateItem>
<CreateItem Include="$(ProjectDir)obj\**\web.config">
<Output TaskParameter="Include" ItemName="ExtraWebConfigsA"/>
</CreateItem>
<Move SourceFiles="@(JavaFolderA)" DestinationFiles="@(JavaFolderA->'$(ProjectDir)%(RecursiveDir)%(FileName).xyz')"/>
<Move SourceFiles="@(ExtraWebConfigsA)" DestinationFiles="@(ExtraWebConfigsA->'$(ProjectDir)%(RecursiveDir)%(FileName).ccc')"/>
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
<CreateItem Include="$(ProjectDir)**\*.xyz">
<Output TaskParameter="Include" ItemName="JavaFolderB"/>
</CreateItem>
<CreateItem Include="$(ProjectDir)obj\**\web.ccc">
<Output TaskParameter="Include" ItemName="ExtraWebConfigsB"/>
</CreateItem>
<Move SourceFiles="@(JavaFolderB)" DestinationFiles="@(JavaFolderB->'$(ProjectDir)%(RecursiveDir)%(FileName).java')"/>
<Move SourceFiles="@(ExtraWebConfigsB)" DestinationFiles="@(ExtraWebConfigsB->'$(ProjectDir)%(RecursiveDir)%(FileName).config')"/>
</Target>
希望这可以节省我花了 3 个小时才弄清楚的时间......
更新:因为这确实增加了构建时间,所以您可以选择添加到顶部的条件以仅在发布样式构建期间执行此检查:
<Target Name="MvcBuildViews" AfterTargets="Build" Condition="'$(MvcBuildViews)'=='true' AND '$(Configuration)' == 'Release'">