1

I want to use JSON configuration for my ASP.NET Core project, as documented here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2

My configuration is called like so:

config
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{commandConfig["Site"]}.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables();

In every appsettings.<site>.json, I could put this:

{
    "Site": "siteName1"
}

But that seems like a waste. What I want is to put this in appsettings.json:

{
    "Site": "$(Site)"
}

And have variable substitution replace $(Site) as appropriate.

Is anything like this possible?

7
  • What is $(Site) supposed to be substituted with? Commented May 9, 2019 at 22:16
  • @DavidG Yes, nothing in this syntax would allow that, so I'd want to do something at the end maybe, like .SubstituteVariables(context); and pass in Site in the context mapping. Commented May 16, 2019 at 13:30
  • Well, let me rephrase where I was going... If you want to substitute with a particular known value, then you already know the value right? Commented May 16, 2019 at 13:39
  • Yes. But if I have 20 sites, managing 20 site config files with not only their Site settings, but also their log location (c:\logs\${Site}), their error email subject ([${Site}] Error Occurred), etc is painful. Better to specify those three once in the appsettings.json` exactly as I wrote in this comment than to manually substitute and maintain 20 site-specific files. Commented May 16, 2019 at 16:49
  • Why do you have 20 config files? This doesn't make sense. You have one config file with replaceable values. For example config.EmailSubject.Replace("$(Site)", theNameOfThisSite) Commented May 16, 2019 at 23:07

2 Answers 2

4

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:

  • Foo = {Bar1}{Bar2}{Bar1}
  • Bar1 = Barista
  • Bar2 = -Jean-

When requesting Foo from the configuration you will get: Barista-Jean-Barista

Sign up to request clarification or add additional context in comments.

Comments

2

I'm not sure sure if that's possible.

I would suggest using an environment variable:

var mySiteVariable = Environment.GetEnvironmentVariable("MySiteVariable");

config
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{mySiteVariable}.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables();

UPDATE:

If you don't want to use another json file, you could use AddInMemoryCollection. Configs added later in the chain will overwrite variables from configs earlier in the chain.

config.SetBasePath(env.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddInMemoryCollection(new Dictionary<string, string>()
    {
        {"MyConfigVariable", "overwrite with this value"}
    });

There is no built-in way to do what you want. You could always read and parse the json file yourself if you really wanted to.

4 Comments

No, that misses the point. I don't want to put something substituted into the configuration filename, I want a setting in the file itself to be substituted.
Like I said, I'm not sure that's possible. The best you could do is "overwrite" a configuration setting by adding new configuration which could be determined dynamically by an environment variable or something. It doesn't even have to be json (see my udpate).
Is it possible to add a layer somewhere to 'interpret' the configuration values, so they can be substituted at runtime?
I'm not exactly sure what you mean by that, but at runtime you can definitely rebuild your config with new values if you wanted to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.