ASP.NET config.json
In my last post, I briefly mentioned config.json in ASP.NET Core. In this post, I am going to drill into configuration and config.json a bit more …
Configuration Source
ASP.NET Core configuration is a lot more flexible - in particular, the way you can have configuration settings coming from different sources. In the following example we are going to get a connection string from config.json.
Firstly, we need to declare a dependency so that we can easily read from config.json:
Then we wire up config.json in the constructor of Startup.cs:
Reading Configuration Settings
Let’s say we have the following config.json that defines the connection to our database:
In order to read the connection string we use IConfiguration.Get():
Notice how you use a colon to get to the nested value in the object graph.
Other Articles
Louis Dejardin explains the reasons for the changes in ASP.NET configuration and this post from Jaspalsinh Chauhan is another great article on this topic.
If you to learn about using React with ASP.NET Core you might find my book useful: