We're building a .net core web api application at the moment and so far it's been great to work with. However we have an issue with our deployment server (Team Studio) being unable to replace the DB connection string inside the appsettings.json file. Is it possible to solve this issue by putting the connection string in the old web.config format so that Team Services can do a replacement? I tried creating one but I'm unable to access the connection string from it using System.Configuration
-
You can parse the XML yourself. – SLaks Nov 08 '17 at 17:14
-
Possible duplicate of [How to read web.config file in .Net Core app](https://stackoverflow.com/questions/46996480/how-to-read-web-config-file-in-net-core-app) – Set Nov 08 '17 at 19:28
2 Answers
The way appsettings.json
is supposed to work is that it should only contain environment-neutral config. Anything specific to a particular environment, such as connection strings, should go into appsettings.{Environment}.json
. That file is then loaded in based on the ASPNETCORE_ENVIRONMENT
environment variable set on the server you're deploying to. In other words, you shouldn't need to replace anything. Just deploy both appsettings.json
and appsettings.{Environment}.json
and make sure that the ASPNETCORE_ENVIRONMENT
variable is set appropriately.

- 232,153
- 36
- 385
- 444
-
That would require any passwords or sensitive data to be checked in to the source repository. Substitution allows for a second layer of security, which is why I prefer to use substitution of appsettings.json in the release pipeline of VSTS. It works great there, but won’t help in this case unfortunately. – joakimriedel Nov 08 '17 at 19:54
-
1That's where an externalized config provider would come into play. You can set the connection using environment variable on the server or use something like Azure Key Vault. – Chris Pratt Nov 08 '17 at 21:27
Fortunately, web.config was removed from .net core. Only one thing that I can recommend is to create power shell script that will replace connection strings in your appsettings.json, and call it using team city agent.

- 1,071
- 2
- 15
- 27