You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过GitHub Action部署ASP.NET Core网站时移除appsettings.Development.json

ASP.NET Core 7部署时排除appsettings.Development.json的可行方案

方案1:在.csproj中标记为“不发布”

这是根源性的解决办法,既能保留团队本地开发共用的配置文件,又能确保发布时自动排除它。直接修改项目的.csproj文件,添加以下配置:

<ItemGroup>
  <Content Include="appsettings.Development.json">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <Publish>Never</Publish>
  </Content>
</ItemGroup>
  • <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>:保证本地开发时文件会复制到输出目录,不影响日常开发
  • <Publish>Never</Publish>:明确告诉MSBuild在发布过程中完全忽略这个文件,不会打包到发布产物里

提交修改后的.csproj到Git,之后无论是本地发布还是GitHub Actions部署,这个文件都不会出现在最终的部署包中。

方案2:通过GitHub Actions移除文件

如果暂时不想修改项目配置,也可以在CI/CD流程中直接删除文件,放在发布步骤之前执行即可。在你的GitHub Actions workflow文件中,添加一个run步骤:

针对Windows runner

- name: 删除appsettings.Development.json
  run: del appsettings.Development.json
  working-directory: ./你的项目路径 # 替换为实际项目根目录路径

针对Linux/macOS runner

- name: 删除appsettings.Development.json
  run: rm appsettings.Development.json
  working-directory: ./你的项目路径 # 替换为实际项目根目录路径

把这个步骤放在actions/checkout@v3之后,dotnet publish或者其他部署相关步骤之前,这样后续的发布流程就不会包含这个文件了。

内容的提问来源于stack exchange,提问作者Adam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 05:31:38