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:

dependency

Then we wire up config.json in the constructor of Startup.cs:

startup

Reading Configuration Settings

Let’s say we have the following config.json that defines the connection to our database:

config.json

In order to read the connection string we use IConfiguration.Get():

read

Notice how you use a colon to get to the nested value in the object graph.

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.