Skip to content

dotnet-labs/ConfigurationBuilderDemos

Repository files navigation

ConfigurationBuilder Demos

Buy Me a Coffee at ko-fi.com

Binding an array

Binding a JSON Array to an Array Object

Recently, when I set a JSON array in the appsettings.json files in an ASP.NET Core project, the result of the final configuration data surprised me. After some googling, I found that there are already many Stack Overflow questions (1, 2, 3, 4, and 5) and GitHub issues (1, 2, 3, 4, 5, and 6) discussing this interesting behavior of configuration inheritance for arrays. I think it is worth writing a short article to describe the "problem" so that you won't stumble on the same stone.

ASP.NET Core uses configuration providers to read configuration key-value pairs from a variety of sources. If there are some key-value pairs with the same keys, then a configuration provider that is added later will override previous key settings. You can read more about configuration in ASP.NET Core in its official documentation, which has covered tons of use cases in detail.

We usually put some generic configuration data in the appsettings.json file, and put environment-specific configurations in the appsettings.{Environment}.json file and/or in the environment variables. In this way, the system can override the settings based on the runtime environment when the application starts. For example, a connection string set in the appsettings.json file will be used for the development environment if its value is not set in the appsettings.Development.json file. On the other hand, if a connection string is set in the appsettings.Production.json file, then the value in the appsettings.json file will be overridden and the value in the appsettings.Production.json file will be used in the production environment.

I also traced back and found this behavior was first proposed in this GitHub issue.

License

Feel free to use the code in this repository as it is under MIT license.

Buy Me a Coffee at ko-fi.com

About

ConfigurationBuilder Demos: APS.NET Core configuration

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages