A Microsoft.Extensions.Configuration based Configuration Token Replacer, which scans all previous ConfigurationProviders and replaces the tokens accordingly.
- Add tokens to your configuration.
Here's an example appsettings.json. The
url
re-uses other configuration settings in the form of tokens by enclosing the keys in{}
{
"ConnectionSettings": {
"protocol": "https",
"hostname": "somehost",
"id": "id1234",
"url": "{ConnectionSettings:protocol}://{ConnectionSettings:hostname}/{ConnectionSettings:id}"
}
}
- Use AddTokenReplacement() on your ConfigurationBuilder
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json"); // from Microsoft.Extensions.Configuration.Json
.AddTokenReplacement();
var Configuration = builder.Build();
var connectionsettings = Configuration.GetSection("ConnectionSettings")["url"];
// returns "https://somehost/id1234"
Add the following to your json configuration and set Replace
to true
or false
:
"TokenReplacementSettings": {
"Replace": true
}
Set OnlySpecific
to true
and add the tokens to be replaced in the Tokens
array:
"TokenReplacementSettings": {
"Replace": true,
"OnlySpecific": true,
"Tokens": [
"ConnectionSettings:protocol",
"ConnectionSettings:hostname"
]
}