Is there a way "out of the box" to have environment variables in appsettings.json values expanded automatically?
To take a contrived example:
{
...
"MyPath1": "C:/MyApp/%ENV%/Myfolder1/MyFile1.dat",
"MyConnectionString": "Server=MyServer%ENV%..."
...
}
My objective is to be able to switch my environment by changing a single environment variable, and have it affect multiple appsetting values, rather than having per-environment configuration files.
UPDATE
I've looked through the source code of JsonConfigurationProvider and as far as I can see there is no such feature out of the box.
I can see that it should be possible by deriving a custom class from JsonConfigurationProvider and overriding the Load() method:
public override void Load()
{
base.Load();
foreach(var key in Data.Keys)
{
Data[key] = Environment.ExpandEnvironmentVariables(key);
}
}
But I'm quite new to .NET Core configuration, which leads to a second question:
How do I get this custom implementation to replace the standard one? I.e. to remove the default appsettings.json and appsettings.environment.json providers and replace by the custom one. Presumably something to be added in Program.cs:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureAppConfiguration(config =>
{
config.??? what do I need here?
}
);