通过工具类 AppSettingsConfig
services.AddSingleton(new AppSettingsConfig(configuration));
using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace XXXXX.Common { public class AppSettingsConfig { public static IConfiguration Configuration { get; set; } public AppSettingsConfig(IConfiguration configuration) { Configuration = configuration; } /// <summary> /// 封装要操作的字符 /// </summary> /// <param name="sections">节点配置</param> /// <returns></returns> public static string GetValue(params string[] sections) { try { if (sections.Any()) { return Configuration[string.Join(":", sections)]; } } catch (Exception) { } return ""; } /// <summary> /// 递归获取配置信息数组 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sections"></param> /// <returns></returns> public static T GetEntity<T>(string sections) { T s = Activator.CreateInstance<T>(); Configuration.Bind(sections, s); return s; } /// <summary> /// 获取连接字符串 /// </summary> /// <param name="name"></param> /// <returns></returns> public static string GetConnectionString(string name) { return Configuration.GetConnectionString(name); } } }
"APISetting": {
"Grant": "client_credentials"
},
使用:
var grant = AppSettingsConfig.GetValue("APISetting:Grant");
"Authentication": {
"QQ": {
"ClientId": "AA",
"ClientSecret": "AA"
}
}
直接读取
options.ClientId = Configuration.GetValue<string>("Authentication:QQ:ClientId");
标签:core,读取,AppSettingsConfig,using,net,Configuration,sections,public,string From: https://www.cnblogs.com/shy1766IT/p/17684909.html