appsettings.json文件
{
"Key": "KeyValue",
"Person": {
"Name": "MyName",
"Age": 10
}
}
读取配置文件
// JSON 文件配置
IConfiguration jsonConfiguration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json") // 添加 Json 文件配置
.Build();
Console.WriteLine(jsonConfiguration["Key"]); // KeyValue
// 使用 ":" 分割 "父节" 与 "子节"
Console.WriteLine(jsonConfiguration["Person:Name"]); // MyName
Console.WriteLine(jsonConfiguration["Person:Age"]); // 10