Are the configuration keys in asp.net core supposed to be case-insensitive?

Viewed 1705

I made a custom configuration provider for my ASP.NET Web App, where I inherit from Microsoft.Extensions.Configuration.ConfigurationProvider, and override the Load() method and init the prop IDictionary<string, string> Data with a new Dictionary<string, string>().

So I have a appsettings.json config file, containing

{
    "Token" : "Token 1"
}

And in my custom provider, I override Token with "Token 2"

I had a bug in my code, where I called configuration["token"] (lowercase), and I noticed the value from the appsettings was loaded, but not the one from my custom provider.

So it seems that the appsettings configuration is case-insensitive, but my custom provider isn't.
So is it supposed to be ? I just created a new Dictionary, I'm not sure if it's possible to make it case-insensitive without writing a lot more code.

1 Answers

The providers that Microsoft provide from NuGet as part of .NET itself use case insensitive keys (code) by default.

Whether or not your custom provider is case sensitive or not is up to you, but I would suggest doing the same and backing the source with a case-insensitive key-value lookup.

If you're using a Dictionary<K, V>, then it's as simple as just using the same constructor that takes a comparer and using StringComparer.OrdinalIgnoreCase.

Related