$mkdir appsettings $cd appsettings $dotnet new $dotnet restoreNow create appsettings.json file:
{ "AppSettings": { "Date": "1900-01-01" } }Date is the setting I am interested in loading from AppSettings section.
Edit project.json file to include "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0" in dependencies section. This is the only extra library we need in order to use ConfigurationBuilder functions.
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true, "outputName": "AppSettings" }, "dependencies": {}, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.1.0" }, "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0" }, "imports": "dnxcore50" } } }Now lets edit the Program.cs file. We are going to load the Date setting and display it in Console. Don't forget to include "using Microsoft.Extensions.Configuration;"
using System; using System.IO; using Microsoft.Extensions.Configuration; namespace ConsoleApplication { public class Program { public static string AppSettingsFile = "appsettings.json"; public static string AppSettingsSection = "AppSettings"; public static string DateSetting = "Date"; public static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(Program.AppSettingsFile, optional: true, reloadOnChange: true); var config = builder.Build(); var appSettings = config.GetSection(Program.AppSettingsSection); string dateString = appSettings[Program.DateSetting]; Console.WriteLine("Date:" + dateString); } } }That is pretty much all. Now let's build and run it.
$dotnet build $dotnet run Date:1900-01-01