.net core 读取appsettings 的配置

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "CxyOrder": "Server=LAPTOP-AQUL6MDE\\MSSQLSERVERS;Database=CxyOrder;User ID=sa;Password=123456;Trusted_Connection=False;"
  },
  "Appsettings": {
    "SystemName": "PDF .NET CORE",
    "Date": "2017-07-23",
    "Author": "PDF"
  },
  "ServiceUrl": "https://www.baidu.com/getnews"
}

 

调用的方法

.net core 读取appsettings 的配置
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

namespace NetCoreOrder.Common
{
    /// <summary>
    /// 读取配置文件
    /// </summary>
    public class AppConfigurtaionServices
    {
        public static IConfiguration Configuration { get; set; }
        static AppConfigurtaionServices()
        {
            //ReloadOnChange = true 当appsettings.json被修改时重新加载            
            Configuration = new ConfigurationBuilder()
              .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
              .Build();            
        }
    }
}
View Code

 

  

读取一级配置节点配置

AppConfigurtaionServices.Configuration["ServiceUrl"];
//得到 https://www.baidu.com/getnews

 

读取二级子节点配置

AppConfigurtaionServices.Configuration["Appsettings:SystemName"];
//得到 PDF .NET CORE
AppConfigurtaionServices.Configuration["Appsettings:Author"];
//得到 PDF

 

 

仅仅为了做个笔记

转自:https://www.cnblogs.com/pudefu/p/7580722.html

 

上一篇:.NET Core在类库中读取配置文件appsettings.json


下一篇:c#-ASP.NET Core中AppSettings.json内的POCO对象数组