There a couple of things described in the articles below:
Automatically set appsettings.json for dev and release environments
launchSettings.json in ASP.NET
Various configuration json files in ASP.NET
The first one is a step by step instruction how to set up appsettings.json for multiple environments; in the 2nd article you find important information about launchSettings.json and how different sets of environment variables can be configured. Here you'll find an example how you can set up environment variables for 3 different configurations like "Development", "Staging" and "Production".
Also there are startup conventions in Startup.cs like "StartupDevelopment" or "StartupProduction" being called if you switched the deployment platform.
Essentially, to set this up, you'll need a combination of these to set up your environment variables properly. I'd use ajawad987's answer but change the environment file path to var environmentFilePath = Path.Combine(binDirectoryPath,$"Environment.{env.EnvironmentName}.txt");
, allowing to set it depending to the environment (the variable comes from Startup(IHostingEnvironment env)
- you might need to add a global property accessible from his code). His code should be added to all projects. Note that you can either set up ASPNETCORE_ENVIRONMENT
as system environment variable or run it via commandline like dotnet run --environment "Development"
.
But note you can also declare environment variables in the launchSetting.json
file, e.g.
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"Hosting:Environment": "Development"
}
},
This JSON file holds project specific settings associated with each profile Visual Studio is configured to use to launch the application.
What is also worthwhile is to look into is the 3rd article about Various configuration json files in ASP.NET, there you can find a description of all json files that exist in current ASP.NET Core projects, and what role they are playing in your project.
It is a pitty that the inventors of .NET core didn't implement that more straightforward!