```Json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"YX": "144@qq.com"
},
"AllowedHosts": "*",
"ServiceUrl": "http://www.cnblogs.com/chuankang/"
}
//读取一级配置节点配置
//AppConfigurtaionServices.Configuration["ServiceUrl"];
//得到 http://www.cnblogs.com/chuankang/
//读取二级配置节点配置
//AppConfigurtaionServices.Configuration["ConnectionStrings:YX"];
//得到"144@qq.com"
//AppConfigurtaionServices.Configuration["ConnectionStrings:LogLevel"];
/*得到"{
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information" }"*/
C#类
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
需引用。
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
/// <summary>
/// 读取配置文件
/// </summary>
public class AppConfigurtaionServices
{
public static IConfiguration Configuration { get; set; }
static AppConfigurtaionServices()
{
//ReloadOnChange = true 当appsettings.json被修改时重新加载
Configuration = new ConfigurationBuilder()
.Add(new JsonConfigurationSource { Path = "jsconfig1.json", ReloadOnChange = true })
.Build();
}
}
注意,如果AppConfigurtaionServices类中抛出FileNotFoundException异常,说明目录下未找到appsettings.json文件,这时请在项目appsettings.json文件上右键——属性——将“复制到输出目录”项的值改为“如果较新则复制”即可。