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

如何在GitHub Workflows中修改.NET Framework 4.8项目构建的web.*.config配置

解决方案

针对.NET Framework 4.8项目构建阶段自动应用Web.config环境转换的需求,可通过以下方案实现,所有配置替换规则都可预置在项目对应配置文件中,无需在Workflow yaml中定义敏感或环境专属变量:

方案1:修改项目文件启用构建时自动转换

直接在Web应用项目的.csproj文件中添加配置,触发构建时自动执行对应环境的Web.config转换:

  1. 右键Web项目选择「卸载项目」,右键选择「编辑.csproj文件」,在第一个<PropertyGroup>节点下新增两个属性:
<TransformWebConfigEnabled>true</TransformWebConfigEnabled>
<TransformWebConfigOnBuild>true</TransformWebConfigOnBuild>
  1. 保存后重新加载项目,提交变更到代码仓库。
  2. Workflow中的构建命令仅需修改Configuration参数即可,无需其他调整:
- name: Build Solution
  run: msbuild MySolution.sln /p:Configuration=Test /p:DeployOnBuild=true

执行构建时会自动匹配Web.Test.Config的转换规则,覆盖到最终生成的Web.config中。

方案2:直接在MSBuild命令中传参(无需修改项目文件)

如果不想改动项目配置,可直接在Workflow的MSBuild参数中添加转换开关,效果和方案1一致:

- name: Build Solution
  run: msbuild MySolution.sln /p:Configuration=Test /p:DeployOnBuild=true /p:TransformWebConfigEnabled=true /p:TransformWebConfigOnBuild=true

前提校验

请提前确认以下配置,避免转换不生效:

  • 你的Web项目中已正确添加Web.Test.Config文件,文件的「生成操作」属性为None,且已设置为依赖Web.config
  • 解决方案和Web项目中都已存在名为Test的生成配置,不是仅在MSBuild命令中自定义配置名
  • 环境专属配置文件中的转换规则已正确使用XDT语法,示例如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="EnvName" value="Test" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
  <connectionStrings>
    <add name="DefaultDB" connectionString="你的测试环境数据库连接串" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
  </connectionStrings>
  <system.web>
    <compilation debug="false" xdt:Transform="SetAttributes" />
  </system.web>
</configuration>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 15:24:03