Skip to content

A Microsoft.Extensions.Configuration based Configuration Token Replacer, which scans all previous ConfigurationProviders and replaces the tokens accordingly.

Notifications You must be signed in to change notification settings

a-khanna/TokenReplacer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TokenReplacer

Build Status

A Microsoft.Extensions.Configuration based Configuration Token Replacer, which scans all previous ConfigurationProviders and replaces the tokens accordingly.


Basic Example

  1. 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}"
        }
    }
  1. 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"

Advanced Usage

Toggle token replacement from json configuration

Add the following to your json configuration and set Replace to true or false:

    "TokenReplacementSettings": {
        "Replace": true
    }
Replace only specific tokens

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"
        ]
    }

About

A Microsoft.Extensions.Configuration based Configuration Token Replacer, which scans all previous ConfigurationProviders and replaces the tokens accordingly.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages