I have added screenshots of a working environment, because it cost me several hours of R&D.
First, add a key to your launch.json
file.
See the below screenshot, I have added Development
as my environment.
Then, in your project, create a new appsettings.{environment}.json
file that includes the name of the environment.
In the following screenshot, look for two different files with the names:
appsettings.Development.Json
appSetting.json
And finally, configure it to your StartUp
class like this:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
And at last, you can run it from the command line like this:
dotnet run --environment "Development"
where "Development"
is the name of my environment.
You can use CreateDefaultBuilder
which will automatically build and pass a configuration object to your startup class:
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
public class Startup
{
public Startup(IConfiguration configuration) // automatically injected
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
/* ... */
}
CreateDefaultBuilder
automatically includes the appropriate appsettings.Environment.json
file so add a separate appsettings file for each environment:
Then set the ASPNETCORE_ENVIRONMENT
environment variable when running / debugging
Depending on your IDE, there are a couple places dotnet projects traditionally look for environment variables:
For Visual Studio go to Project > Properties > Debug > Environment Variables:
For Visual Studio Code, edit .vscode/launch.json
> env
:
Using Launch Settings, edit Properties/launchSettings.json
> environmentVariables
:
Which can also be selected from the Toolbar in Visual Studio
Using dotnet CLI, use the appropriate syntax for setting environment variables per your OS
Note: When an app is launched with dotnet run,
launchSettings.json
is read if available, andenvironmentVariables
settings in launchSettings.json override environment variables.
Host.CreateDefaultBuilder
work?.NET Core 3.0 added Host.CreateDefaultBuilder
under platform extensions which will provide a default initialization of IConfiguration
which provides default configuration for the app in the following order:
appsettings.json
using the JSON configuration provider.appsettings.Environment.json
using the JSON configuration provider. For example:
appsettings.Production.json
orappsettings.Development.json
- App secrets when the app runs in the Development environment.
- Environment variables using the Environment Variables configuration provider.
- Command-line arguments using the Command-line configuration provider.
In ASP.NET Core you should rather use Environment Variables instead of build configuration for proper appsettings.json
Right click on you project > Properties > Debug > Environment Variables
ASP.NET Core will use the appropriate appsettings.json file:
Now you can use that Environment Variable like this:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
Note: If you use @Dmitry's answer, you can run into problems eg. when overriding appsettings.json values on Azure.
You can make use of environment variables and the ConfigurationBuilder
class in your Startup
constructor like this:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
this.configuration = builder.Build();
}
Then you create an appsettings.xxx.json
file for every environment you need, with "xxx" being the environment name. Note that you can put all global configuration values in your "normal" appsettings.json
file and only put the environment specific stuff into these new files.
Now you only need an environment variable called ASPNETCORE_ENVIRONMENT
with some specific environment value ("live", "staging", "production", whatever). You can specify this variable in your project settings for your development environment, and of course you need to set it in your staging and production environments also. The way you do it there depends on what kind of environment this is.
UPDATE: I just realized you want to choose the appsettings.xxx.json
based on your current build configuration. This cannot be achieved with my proposed solution and I don't know if there is a way to do this. The "environment variable" way, however, works and might as well be a good alternative to your approach.
You may use conditional compilation:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
#if SOME_BUILD_FLAG_A
.AddJsonFile($"appsettings.flag_a.json", optional: true)
#else
.AddJsonFile($"appsettings.no_flag_a.json", optional: true)
#endif
.AddEnvironmentVariables();
this.configuration = builder.Build();
}
Just an update for .NET core 2.0 users, you can specify application configuration after the call to CreateDefaultBuilder
:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(ConfigConfiguration)
.UseStartup<Startup>()
.Build();
static void ConfigConfiguration(WebHostBuilderContext ctx, IConfigurationBuilder config)
{
config.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", optional: false, reloadOnChange: true)
.AddJsonFile($"config.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
}
}
Create multiple appSettings.$(Configuration).json
files like:
appSettings.staging.json
appSettings.production.json
Create a pre-build event on the project which copies the respective file to appSettings.json
:
copy appSettings.$(Configuration).json appSettings.json
Use only appSettings.json
in your Config Builder:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
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