Add appsettings.json to a .Net Core Console Application

Firstly add the appsettings.json file to the solution

1
2
3
4
5
{
"AppSettings": {
"Key1": "This is the value for Key1"
}
}

Set the .json file to copy to the output directory (this is the bin folder)

Copy if newer

Add Microsoft.Extensions.Configuration.Json from Nuget

Microsoft.Extensions.Configuration.Json

The read the file

1
2
3
4
5
6
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
.Build();

var key1 = config["AppSettings:Key1"];
Console.WriteLine(key1);

Run the code!

Run the code