0

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.

  • Does anybody else have no idea? Any help pls? –  May 06 '20 at 14:41
  • 1
    Is this for ASP.NET or ASP.NET Core? You've tagged both. – Chris Pratt May 06 '20 at 16:15
  • @ChrisPratt Hi Chris, sorry I forget the difference. It is ASP.NET **MVC**, I am waiting for your valuable answer and comments. Thanks in advance. –  May 06 '20 at 19:27
  • @ChrisPratt On the other hand, as far as I know we can use transformation in `web.debug.config` and `web.debug.config` both, or in one of them according to our needs. I mean that, assume we have a connection string for **prod** database in `web.config` and need to use connection string for **test** database while debugging. In that case we should define test connection string (with transformed form of course) `web.debug.config` and we do not anything in `web.release.config`. Is that true? –  May 06 '20 at 20:56
  • @ChrisPratt Alsı I realized that when debugging in VS, it uses only `web.config` configuration even if I select Release. But I expect to use `web.release.config` when selecting release. Is it normal? –  May 06 '20 at 23:19
  • @ChrisPratt I explained it on [Running app in release mod cannot use `web.release.config` in Visual Studio](https://stackoverflow.com/questions/61647368/running-app-in-release-mod-cannot-use-web-release-config-in-visual-studio). –  May 07 '20 at 00:00
  • @ChrisPratt Are you there? –  May 08 '20 at 10:48

1 Answers1

0

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

Shawn Paige
  • 338
  • 1
  • 8
  • Thanks for reply, I read that article and it is updated. However, it uses a extension and does not mention three config files that I asked in my question. Instead, I am looking for an explanation easy step-by-step without extension. On the other hand, in that article it uses isProd, but many other articles don't use and only use key name. –  May 06 '20 at 19:53
  • On the other hand, as far as I know we can use transformation in `web.debug.config` and `web.debug.config` both, or in one of them according to our needs. I mean that, assume we have a connection string for **prod** database in `web.config` and need to use connection string for **test** database while debugging. In that case we should define test connection string (with transformed form of course) `web.debug.config` and we do not anything in `web.release.config`. Is that true? –  May 06 '20 at 20:55
  • Any help please? –  May 08 '20 at 10:48
  • I have only used web.config transforms for publishing to various environments as outlined in https://devblogs.microsoft.com/aspnet/asp-net-web-projects-web-debug-config-web-release-config/. I typically have the connection string(s) in the base web.config pointing to the development database(s), then transform(s) in web.release.config to replace the connection string(s) with the production value(s). When I need to debug against a database other than development, I temporarily change the values in the base web.config. Is that what you are trying to do? We have one transform per environment. – Shawn Paige May 08 '20 at 14:17
  • Your last comment is much more detailed and clarified me, thanks. In this scene, could you confirm the following issues pls? There are 3 web config as you know: base (`web.config`), debug (`web.debug.config`) and release (`web.release.config`). Assume that I have a **dev** and **prod** environment and **2** different **connectionStrings** for them. >>> –  May 08 '20 at 14:30
  • **1)** Normally I can define dev or prod connection in the base config according to use during development. Is that true? –  May 08 '20 at 14:30
  • **2)** If I use dev connection string in the base config and want to publish prod connection string to the prod environment, then I just need to define it in the prod config by transforming. There is no need to define and transform it on the dev config, because I have already had it in the base config and I can use it for development. Is that right? –  May 08 '20 at 14:30
  • **3)** On the other hand, in order to prevent mistake, I can also define dev config in the dev connection string besides defining prod connection string in the prod config in order to prevent any mistake (prevent using prod database during development or publishing dev test server). Is that right? –  May 08 '20 at 14:30
  • **4)** We can select which config we use before publishing (Release, Debug, etc.). What about debugging the application? I mean, when running the app in VS, I am expecting that if I select **Debug**, it will use debug config, and if I select **Release** and run the app, it will use release config, but as far as I see it does not. Is it normal? –  May 08 '20 at 14:33
  • 1) Yes. However, at a lot of places, it is not possible to access production databases from a developer's computer due to firewall security. Often, a production database backup is restored to the development server and you change your connection string to point to that. – Shawn Paige May 08 '20 at 17:41
  • 2) Yes, 3) Yes - this can also be done by not checking temporary testing changes into source control. 4) By default, the web.config transforms are only used for publishing, not local debugging. – Shawn Paige May 08 '20 at 17:42
  • Many thanks for your answers, they are really useful for me. However, I am not sure your **By default, the web.config transforms are only used for publishing, not local debugging.** sentence. Can we also set it to use local debugging? Because I remember as if there were a project that use debug or release config according to the selected config (Debug or Release). Any idea? –  May 08 '20 at 17:54
  • Personally, I have never done that. Others report they have https://gist.github.com/EdCharbeneau/9135216 – Shawn Paige May 08 '20 at 18:56
  • Thanks a lot for your helps. –  May 08 '20 at 21:06