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:
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.
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.
With app settings we have to craft the key name to match the structure of the json file. So:
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:
This would look like:
Very cool stuff! For more reading check these posts out: