NetCore 如何简单快速的读取配置文件(appsettings.json)

2020-03-12 13:59发布

问题:

假定有这样一个JSON

{
  "appSettings": {
    "Keep": "1",
    "DefaultConnStr": "DeafultConnection"
  },
  "ConnectionStrings": {
    "DeafultConnection": "Provider=mssql;",
    "ModuleConnection": "Provider=mssql;",
    "WeChatConnection": "Provider=mssql;",
    "MySqlConnection": "Provider=mysql;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

 

现在需要读取  ConnectionStrings  中所有节点 (且事先并不知道这个节点有几个)

 

在.NetFramework中知道如何操作 , 突然切换过来有点不知所措 . 

回答1:

var connectionStringsList = builder.Build().GetSection("ConnectionStrings").AsEnumerable(true);

foreach(var item in connectionStringsList )

item.key //DeafultConnection
item.value // Provider=mssql;



回答2:

参考 Get Multiple Connection Strings in appsettings.json without EF