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 …
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:
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():