ASP.NET Web.Debug和Web.Release文件转换



首先,我知道有几个关于这个问题的页面,例如Web.Config调试/发布、Web.Config转换语法现在适用于任何XML配置文件和Web.Config文件转换。但是它们中的大多数都已经过时了,并且没有清楚地提到所有三个文件:Web.configWeb.Debug.configWeb.Release.config

因此,假设我对Web.config有以下设置:

Web.config:

<appSettings>
<add key="ClientId" value="xxxxx"/>
<add key="ClientSecret" value="xxxxx"/>
</appSettings>

我想以以下方式在调试和发布中使用这些设置:

Web.Debug.config:

<appSettings>
<add key="ClientId" value="ddddd"/>
<add key="ClientSecret" value="ddddd"/>
</appSettings>

Web.Release.config:

<appSettings>
<add key="ClientId" value="rrrrr"/>
<add key="ClientSecret" value="rrrrr"/>
</appSettings>

1(准确执行此操作的步骤是什么?我认为,当调试发布时,这些设置会根据我在Visual Studio运行和发布对话框中选择的"调试"或"发布"自动使用。这是真的吗?

2(在移动到Web.Debug.config和Web.Release.config之后,我应该从Web.config中删除这些设置吗?

3(VS中"发布"对话框的"配置"字段中的"测试"选项是什么?

如有任何帮助,我们将不胜感激。

我建议阅读web.config转换工作原理的概述:

https://blog.elmah.io/web-config-transformations-the-definitive-syntax-guide/

通常,Web.*.config文件将根据Visual Studio中选定的发布配置对Web.config文件进行更改。例如,如果您想更新/替换调试发布中的值,那么您的Web.debug.config文件应该如下所示:

<configuration xmlns:xdt="...">
<appSettings>
<add key"ClientId" value="ddddd" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
<add key"ClientSecret" value="ddddd" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>

以下是有关这些功能的最新Microsoft文档:https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-3.1

最新更新