static void

ASP.Net Core Configuration Environments

In .net framework we have configurations - "debug" and "release"- and you could add more (via Visual Studio Build/configuration manager. automatically transformed the web.config xml so you could have different sets of appsettings/connection strings etc.

Environments is the core version of this. The term configuration still exists, in visual studio and "dotnet build -c release" / "dotnet publish -c Prod"

One major difference is the appsettings.json are NOT transformed like the web.config (but see SlowCheetah below). Therefore the typical deployment will have the original (debug) appsettings.json AND all the appsettings.env.json. The environment setting is used to select which env.config to load.

A buildserver can rewrite the appsettings.json (devops file transforms) but changing the build pipeline isn't quite as easy as changing a transform.


Environments are set in one (or more!) of:

Environment Variables

You can set environment variables in the web.config

                <add name="aspNetCore" path="*" verb="*"
                     modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            <aspNetCore processPath="dotnet" arguments=".\Net6Web.dll"
                        stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout"
                    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="TST" />

You can also set them in launchSettings.json:

"environmentVariables": {

Set in code

Using the environment

It's just a free text string property: IWebHostEnvironment/IHostingEnvironment.EnvironmentName

<Project Sdk="Microsoft.NET.Sdk.Web">


    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='TST|AnyCPU'">

During initialization

In Program.cs, the WebApplication exposes IWebHostEnvironment via .Environment

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())

In the application (using DI)

private readonly IWebHostEnvironment _webHostEnvironment;

public RocketController(
    ILogger<RocketController> logger
    IWebHostEnvironment webHostEnvironment)
    _logger = logger;
    _webHostEnvironment = webHostEnvironment;

public IActionResult LaunchRocket()
    if (_webHostEnvironment.IsEnvironment("TST"))
        return Content("In TST we don't launch rockets");


Properties\launchSettings.json is optional, and never deployed. From project properties, the Debug menu item has a link ("Open debug launch profiles UI") - it is also available on the toolbar Run menu (with a wrench symbol).

You can set the environment either as environmentVariables or as commandLineArgs (both shown here, for overkill!).

  "profiles": {
    "MyConsoleApp": {
      "environmentVariables": {

Config Transforms

In framework, .config xml can be transformed using xdt. In core, you can use Microsoft's Microsoft.VisualStudio.SlowCheetah (build-time nuget) which includes a json transformer. In the .csproj, appsettings.json has a subnode <TransformOnBuild>true while the DependentUpon appsettings.env.json has a <IsTransformFile>true subnode .

In Visual Studio right-click the appsettings.json and use SlowCheetah "Add Transforms". The transforms for all the configurations are created. They are NOT set to "Copy to output directory". But when you use publish (via Visual Studio or "dotnet publish -c Acc") it will apply the transform- the appsettings.json is the only file and it is transformed.