Override Connection Strings & App Settings in ASP.NET Core and Azure App Service

If you’ve deployed a website to Azure using App Services, you know you can override app settings and connection strings in the web.config using environment variables. This is a very handy feature since you can scrap web.config transforms all together if these are the only areas you need to touch.

Since ASP.NET Core does not use web.configs, at least not in a traditional way, how do we override these settings now? Well, pretty much in the exact same way, just with a different format. First let’s look at the new appsettings.json file that will be in your Core app:

{
“Logging”: {
“IncludeScopes”: false,
“LogLevel”: {
“Default”: “Debug”,
“System”: “Information”,
“Microsoft”: “Information”
}
},
“ConnectionStrings”: {
“ConnectionString1”: “localhost-connection-string”
},
“AppSettings”: {
“Environment”: “localhost”,
“NonSlot1”: “localhost1”,
“NonSlot2”: “localhost2”
}
}

In this example, I simple have a Logging section (in there by default), a ConnectionStrings section (this must be named ‘ConnectionStrings’ if you truly want to use it as such), and an AppSettings section (you can name this anything, but I like to keep it familiar).

To override these in your Azure App Service web app, simply navigate to your web app in Azure (or the deployment slot, as in this image), and open up the Application Settings and scroll down to the App Settings and Connection strings sections.

override-appsettings-core-azure-1

Connection Strings

Connection strings are simple. Since Core encourages you to use a section called ConnectionStrings and only put connection strings in it, you can simply reference the connection string name without any other gunk on it.

App Settings

With app settings we have to craft the key name to match the structure of the json file. So:

“AppSettings”: {
“Environment”: “localhost”,
“NonSlot1”: “localhost1”,
“NonSlot2”: “localhost2”
}

Becomes:

  • AppSettings:Environment
  • AppSettings:NonSlot1
  • AppSettings:NotSlot2

You simply add a colon between the parent and child items. A double underscore also works: AppSettings__Environment.

What’s nice about this is, we can now have nested app settings (if you like) and easily reference them using this new structure and naming convention:

“AppSettings”: {

“AwesomeSettings”: {
“Environment”: “awesomehost”,
“NonSlot1”: “awesomehost1”,
“NonSlot2”: “awesomehost2”
}

“CoolSettings”: {
“Environment”: “coolhost”,
“NonSlot1”: “coolhost1”,
“NonSlot2”: “coolhost2”
}

}

This would look like:

  • AppSettings:AwesomeSettings:Environment
  • AppSettings:CoolSettings:Environment

Very cool stuff! For more reading check these posts out: