Setting environments in ASP.NET Core

Dominic Burford
2 min readJul 17, 2018

--

I recently had a need to setup different environments for our ASP.NET Core 2.1 web application. When we run the app from our development machines, we need an environment where we can diagnose and debug the code. We also use a different endpoint for the services (we have development, staging and production endpoints for our ASP.NET Web API services which are consumed by the development, staging and production versions of the web app respectively). On top of that we also use a different Azure ADB2C(Azure Active Directory Business-to-Consumer) directories for our identity provisioning. We have one for development, staging and production.

So we need to separate out these different environments when running the application so that the development, staging and production settings are consumed appropriately by the application.

Thankfully ASP.NET Core makes this very straight-forward. Right out of the box ASP.NET Core supports 3 environments. Development, Staging and Production. The values for these environments are contained in json files called appsettings.<environment>.json e.g. appsettings.Development.json, appsettings.Staging.json and appsettings.Production.json.

If you have common settings that apply irrespective of the environment, then these can be specified in the default appsettings.json file. The environment specific settings will then be merged into this default file at runtime. So for example, if you use the same instance of Application Insights across all the environments, then specify these once in the default appsettings.json file. Then in the Development, Staging and Production versions of the appsettings.json file, specify those settings that are specific to that environment.

Next you need to tell the ASP.NET Core runtime execution engine what environment to use. For development, you set this inside Visual Studio (right click on the project -> Properties -> Debug). You will see an environment variable called ASPNETCORE_ENVIRONMENT. This will be set to Development. This tells the ASP.NET Core runtime to use the Development environment settings. So any settings that are contained within your default appsettings.json file will be merged with those of appsettings.Development.json.

N.B. specific settings overwrite general ones. So if there are any settings that are in both files, the environment specific ones will be taken.

Setting the environment for development is straight forward, as it’s done within Visual Studio. How do you set the environment for a deployed application on a Windows server or Azure?

This is also straight forward and can be found in this article[^].

N.B. It is by setting the ASPNETCORE_ENVIRONMENT to the appropriate value that determines which environment the ASP.NET Core runtime uses.

For the current ASP.NET Core web application I am developing I have an appsettings.json (which stores our Azure Application Insights settings), appsettings.Development.json, appsettings.Staging.json and appsettings.Production.json. Ther latter 3 store the values that are specific to those particular environments (debuggings settings, logging settings etc).

ASP.NET Core makes is simple and easy to configure your application for different environments, and these can be easily set for Windows / IIS environments and / or Azure environments.

--

--

Dominic Burford

A father, cyclist, vegetarian, atheist, geek and multiple award winning technical author. Loves real ale, fine wine and good music. All round decent chap.