ASP.NET 如何在发布应用程序期间在 web.config 中设置变量

ASP.NET how to set a variable in web.config during publising an app

我有一个 ASP.NET 应用程序,我想在发布应用程序期间根据环境(如生产、质量等....)设置一个变量 我在配置部分的 appsettings 部分的 web.config 文件中创建了一个变量,如下所示:

<add key="RunningEnvironment" value="dev"/>

我在部署 dev 和 prod 时有两个配置文件

如何根据我在部署时选择的配置文件在 web.cong 中设置 RunningEnvironment 变量?

看看web.config transformations

在生产环境的 Web.Production.Config 中设置:

<add xdt:Transform="Replace" xdt:Locator="Match(key)" key="RunningEnvironment" value="prod"/>

您最好了解的一个实用提示是 预览转换 Visual Studio 中 Web.<ConfigurationName>.Config 文件的下拉菜单选项。在那里您会看到所有将要生效的更改。