I've been learning about ASP.NET environment variables and configurations.
I see that, by default, any settings placed in appsettings.Development.json will override the same settings in appsettings.json when running in my own local environment. And that appsettings.Development.json is not deployed and so does not override any settings when deployed.
My question: Does anyone know a way to use this same mechanism for multiple deployed configurations?
For example, I have a website and a demo version of the website. Both use the same code base but a couple of settings need to be different for the demo site. It would be great if I could create appsettings.Demo.json and place any settings in there that need to be overridden just for the demo site.
Currently, I just set an IsDemoSite
setting in the file. This works but it would be much more elegant if I could have different configuration settings.
I haven't been able to find any documentation about having different configurations when deployed.
Also, one of the differences is that the demo site uses a different database connection string. And so I would need to be able to set which version I'm working on when running migrations, etc.
Well, I had the same thing to do - Your on track with custom appsettings files.
In my case, I wanted a separate development, production, wsl and docker configuration. Solution - given that I was running prod and dev on separate virtual instances, was to use old and true
ASPNETCORE_ENVIRONMENT
variable.I created required variations of
appsettings
(appsettings.docker etc) and set up aforementioned variable for given files.In addition as my app starts, it grabs the path to the correct file (part of the name is taken from
ASPNETCORE_ENVIRONMENT
) and loads it as normal json file to.ConfigureAppConfiguration
method ofCreateHostBuilder
. It works perfectly for my requirements - should be good for You.You can find example here.
As for setting up
ASPNETCORE_ENVIRONMENT
- depends on the host system that You are on:for Windows:
Follow this guide - it will guide you through global and in-app settings
for WSL - in Your
launchSettings.json
: