参考微软官方说明:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration
具体为:
读取方法见下图中标红的格式,数字代表顺序,分号代表级别
Console.WriteLine($"option1 = {Configuration["option1"]}"); Console.WriteLine($"option2 = {Configuration["option2"]}"); Console.WriteLine( $"suboption1 = {Configuration["subsection:suboption1"]}"); Console.WriteLine(); Console.WriteLine("Wizards:"); Console.Write($"{Configuration["wizards:0:Name"]}, "); Console.WriteLine($"age {Configuration["wizards:0:Age"]}"); Console.Write($"{Configuration["wizards:1:Name"]}, "); Console.WriteLine($"age {Configuration["wizards:1:Age"]}");
对应的appsetting文件:
{ "option1": "value1_from_json", "option2": 2, "subsection": { "suboption1": "subvalue1_from_json" }, "wizards": [ { "Name": "Gandalf", "Age": "1000" }, { "Name": "Harry", "Age": "17" } ] }