public class MySettings
{
public string ApplicationName { get; set; }
}
public class MyModuleSettings
{
public string ModuleName { get; set; }
}
{
"ApplicationName": "My sample application",
"SectionA": {
"ModuleName": "My sample module"
}
}
public class Program
{
public static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
IConfigurationRoot configuration = builder.Build();
var settings = new MySettings();
configuration.Bind(settings);
var moduleSettings = new MyModuleSettings();
configuration.GetSection("SectionA").Bind(moduleSettings);
Console.WriteLine($"My application name is ‘{settings.ApplicationName}‘");
Console.WriteLine($"My module name is ‘{moduleSettings.ModuleName}‘");
Console.ReadLine();
}
}
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.Extensions.Configuration": "1.1.0",
"Microsoft.Extensions.Configuration.Binder": "1.1.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
"Microsoft.Extensions.Configuration.Json": "1.1.0",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}