Imagine the following appSettings.json
file:
{
"GlobalSettings": {
"BaseUrl": "http://example.com:5000"
},
"NavigationSettings": {
"LoginUrl": "{GlobalSettings:BaseUrl}/Login"
}
}
What I'd like to do is to automatically replace the value of GlobalSettings:BaseUrl
within the value of NavigationSettings:LoginUrl
. I know one can use appSettings.{env}.json
to override specific keys, but that overrides the entire value, not just part of it.
I could work around this by using an extension method something like the following, but this would require the programmer to remember to call the extension method everywhere they might want to have a config value replaced.
ReplaceConfigValues(this string input, IConfiguration config) { /*...*/ }
Can anyone suggest any alternative methods I could use, or are there some new .Net Core features I might be missing?
Even something like referencing a environment variable within the config value would be useful, but I don't know of a way to do that either. E.g.
"LoginUrl": "${ENV_GLOBAL_BASE_URL}/Login"
The comment from Tseng is true, there is nothing built-in yet. I created a library for that purpose it does exactly what you expect. See: https://github.com/molinch/ConfigurationSubstitutor You just need to register it as another configuration source.
For example if you have these three entries defined in the configuration:
When requesting Foo from the configuration you will get: Barista-Jean-Barista
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