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.