在发布时替换 asp.net 核心与 2017 中的 web.config

replace web.config in asp.net core vs 2017 on publish

我正在尝试用生产版本替换 web.config。

我在 ~\production\web.config 中有生产版本(tilda 作为我项目文件夹的根目录)。

我在迁移文档中找到了这个

<ItemGroup>

  <Content Include="Views\**\*" PackagePath="%(Identity)" />

  <None Include="notes.txt" CopyToOutputDirectory="Always" />
  <!-- CopyToOutputDirectory = { Always, PreserveNewest, Never } -->

  <None Include="publishnotes.txt" CopyToPublishDirectory="Always" />
  <!-- CopyToPublishDirectory = { Always, PreserveNewest, Never } -->
</ItemGroup>

我试过用这样的东西

<ItemGroup>   
     <None Include="_production\web.config" CopyToPublishDirectory="Always" /> 
</ItemGroup>

但这样文件夹和文件都会复制到发布目录。它是映射和发布的组合,不幸的是文档缺少示例,因此我尝试 guess/figure 通过将映射示例与发布相结合来解决这个问题。

首先我尝试了这个:

<None Include="_production\web.config" CopyToPublishDirectory="Always"  PackagePath="in/web.test"/>

所以我期待在发布目录中看到一个 in 文件夹,但没有。

我试过了

<None Include="_production\web.config" CopyToPublishDirectory="Always"  PublishPath="in/web.test"/>

但效果不佳。

您可以找到迁移文档here

不确定这是否是最正确的方法,但您可以直接使用 Copy 任务来实现:

<Target Name="CustomScript" AfterTargets="GeneratePublishRuntimeConfigurationFile">
    <Copy SourceFiles="_production\web.config" 
          DestinationFolder="$(PublishDir)" 
          OverwriteReadOnlyFiles="true"
          SkipUnchangedFiles="false" />
</Target>
  • AfterTargets="GeneratePublishRuntimeConfigurationFile" 因为我们只想在默认 web.config 将在发布文件夹
  • 中生成后才进行替换
  • OverwriteReadOnlyFiles="true" - 没有这个 web.config 无法替换