Search code examples
c#asp.netasp.net-mvcvisual-studioweb-config

ASP.NET Web.Debug and Web.Release file transformations


First of all I know there are several pages about this issue e.g. Web.Config Debug/Release, Web.config Transformation Syntax now generalized for any XML configuration file and Web.config File Transformations. But most of them are outdated and does not mentioned clearly about all of the three files: Web.config, Web.Debug.config, Web.Release.config.

So, assume that I have the following settings for Web.config:

Web.config:

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

And I want to use these settings in debug and release in the following ways:

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) What is the procedures to perform this accurately? I think while debugging and publishing, these settings are used automatically according to my selection Debug or Release in Visual Studio run and publish dialog. Is that true?

2) Should I remove these settings from Web.config after moving to Web.Debug.config and Web.Release.config?

3) What is the Test selection in the Configuration field of the Publish dialog in VS?

Any help would be appreciated.


Solution

  • I would recommend reading an overview of how web.config transforms work:

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

    In general, the Web.*.config files will make changes to the Web.config file depending on the selected publish configuration in Visual Studio. For example, if you want to update/replace a value in a debug publish, your Web.Debug.config file should look like:

    <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>
    

    Here is the current Microsoft documentation on how these work: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-3.1