Is there a way to change the environment settings when deploying ASP.NET Core application (like config file transformations using debug/release build)?
What would be the best approach for maintaining multiple environment settings in .NET Core applications (something similar to <appSettings file="local.config">
for local, staging and production)?
Set Your Local Environment Variable In Visual Studio, right-click on your console application project and go to properties. Click on the 'Debug' option in the left navigation. Add a new environment variable called ASPNETCORE_ENVIRONMENT and set the value to Development.
The configuration system in ASP.NET Core is built up of layers of configuration values, compiled from multiple sources. You can load values from JSON files, XML files, environment variables, or you can create a custom provider to load values from pretty much anywhere.
The central configuration file is the appsettings.json
and you can have multiple files, like appsettings.Production.json
etc, which will be loaded and override settings from the appsettings.json
.
For example
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(hostEnv.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostEnv.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
All you need to get this working is the environment variable for setting the environment type (see documentation here).
You can also have environment variables that override, if you add AddEnvironmentVariables()
to your configuration builder. So if you have a appsettings.json of
{
"Data" {
"Default" {
"ConnectionString" : "..."
}
}
}
and want to override that via environment Variable, you'd set up an environment variable called "Data:Default:ConnectionString" and it's value will override the settings in the appsettings.config and appsettings.Production.config (assuming your .AddEnvironmentalVariables()
is called after .AddJsonFile()
- Last registration with the matching key wins) with the value from the environment variable.
You can find more in the official documentation here.
Since in the comments some understand this as the only way to set the environment, there are many ways to set environment variable (most of it is documented in Use multiple environments in ASP.NET Core), all ultimately boiling down to being an environment variable, just within a different scope:
set ASPNETCORE_ENVIRONMENT=Development
or $Env:ASPNETCORE_ENVIRONMENT = "Development"
on powershell, export ASPNETCORE_ENVIRONMENT = Development
on linux)ASPNETCORE_ENVIRONMENT=Production dotnet MyApp.dll
)Docker container, i.e. via docker-compose.yaml
web:
environment:
- ASPNETCORE_ENVIRONMENT=Debugging
docker run -e ASPNETCORE_ENVIRONMENT=Debugging
in IIS via web.config.
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="true" >
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
dotnet run --launch-profile Development
They all change/set the environment variable in a specific scope (globally, locally to a container, inside the app pool, per execution etc.). Choose one which suits your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With