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

使用Visual Studio IIS调试时,如何管理ASP.NET Core的web.config?

推荐处理方案

针对你的需求——保留本地IIS调试配置的同时,让Release构建生成不带环境变量的默认web.config,以下是两种实用方案,优先推荐第一种:

方案一:通过项目配置实现自动生成纯净web.config(推荐)

这个方法能彻底分离本地调试配置和发布配置,无需额外维护转换文件:

  1. 删除项目目录中本地调试时自动生成的web.config文件
  2. 打开项目的.csproj文件,添加以下配置节点:
<PropertyGroup>
  <!-- 强制发布时生成默认的web.config -->
  <GenerateWebConfig>true</GenerateWebConfig>
  <!-- 禁止将launchSettings.json中的环境变量写入发布用的web.config -->
  <IncludeLaunchSettingsEnvironmentVariables>false</IncludeLaunchSettingsEnvironmentVariables>
</PropertyGroup>

配置生效后:

  • 本地调试时,Visual Studio仍会根据launchSettings.json的配置生成临时web.config,不影响你的本地调试流程
  • 发布Release版本时,会自动生成不带任何环境变量的默认web.config,完全符合你需要的默认行为

方案二:使用Web配置转换(适合需自定义发布配置的场景)

如果需要针对发布版本做更多自定义配置,可以用配置转换的方式清除环境变量:

  1. 在项目上右键 → 「添加」→ 「新建项」→ 选择「Web配置转换」,创建web.Release.config文件
  2. 在web.Release.config中添加以下转换规则:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <aspNetCore>
        <!-- 清除web.config中的environmentVariables节点 -->
        <environmentVariables xdt:Transform="Remove" />
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

发布Release版本时,Visual Studio会自动应用该转换,移除web.config中的环境变量配置,生成符合要求的发布文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 12:00:59