WebApi读取配置文件帮助类
1 /// <summary> 2 /// appsettings.json操作类 3 /// </summary> 4 public class AppSettings 5 { 6 static IConfiguration Configuration { get; set; } 7 static string contentPath { get; set; } 8 9 public AppSettings(string contentPath) 10 { 11 string Path = "appsettings.json"; 12 Configuration = new ConfigurationBuilder() 13 .SetBasePath(contentPath) 14 .Add(new JsonConfigurationSource 15 { 16 Path = Path, 17 Optional = false, 18 ReloadOnChange = true 19 }).Build(); 20 } 21 22 public AppSettings(IConfiguration configuration) 23 { 24 Configuration = configuration; 25 } 26 /// <summary> 27 /// 封装要操作的字符 28 /// </summary> 29 /// <param name="sections">节点配置</param> 30 /// <returns></returns> 31 public static string app(params string[] sections) 32 { 33 try 34 { 35 36 if (sections.Any()) 37 { 38 return Configuration[string.Join(":", sections)]; 39 } 40 } 41 catch (Exception) 42 { 43 44 } 45 46 return ""; 47 } 48 49 /// <summary> 50 /// 递归获取配置信息数组 51 /// </summary> 52 /// <typeparam name="T"></typeparam> 53 /// <param name="sections"></param> 54 /// <returns></returns> 55 public static List<T> app<T>(params string[] sections) 56 { 57 List<T> list = new List<T>(); 58 Configuration.Bind(string.Join(":", sections), list); 59 return list; 60 } 61 }
第二种
1 public class AppSettings 2 { 3 static IConfiguration Configuration { get; set; } 4 5 public AppSettings(IConfiguration configuration) 6 { 7 Configuration = configuration; 8 } 9 10 /// <summary> 11 /// 封装要操作的字符 12 /// </summary> 13 /// <param name="sections">节点配置</param> 14 /// <returns></returns> 15 public static string App(params string[] sections) 16 { 17 try 18 { 19 if (sections.Any()) 20 { 21 return Configuration[string.Join(":", sections)]; 22 } 23 } 24 catch (Exception ex) 25 { 26 Console.WriteLine(ex.Message); 27 } 28 29 return ""; 30 } 31 32 /// <summary> 33 /// 递归获取配置信息数组 34 /// </summary> 35 /// <typeparam name="T"></typeparam> 36 /// <param name="sections"></param> 37 /// <returns></returns> 38 public static List<T> App<T>(params string[] sections) 39 { 40 List<T> list = new(); 41 try 42 { 43 if (Configuration != null && sections.Any()) 44 { 45 Configuration.Bind(string.Join(":", sections), list); 46 } 47 } 48 catch 49 { 50 return list; 51 } 52 return list; 53 } 54 public static T Bind<T>(string key, T t) 55 { 56 Configuration.Bind(key, t); 57 return t; 58 } 59 60 61 public static T GetAppConfig<T>(string key, T defaultValue = default) 62 { 63 T setting = (T)Convert.ChangeType(Configuration[key], typeof(T)); 64 var value = setting; 65 if (setting == null) 66 value = defaultValue; 67 return value; 68 } 69 70 /// <summary> 71 /// 获取配置文件 72 /// </summary> 73 /// <param name="key">eg: WeChat:Token</param> 74 /// <returns></returns> 75 public static string GetConfig(string key) 76 { 77 return Configuration[key]; 78 } 79 80 /// <summary> 81 /// 获取配置节点并转换成指定类型 82 /// </summary> 83 /// <typeparam name="T">节点类型</typeparam> 84 /// <param name="key">节点路径</param> 85 /// <returns>节点类型实例</returns> 86 public static T Get<T>(string key) 87 { 88 return Configuration.GetSection(key).Get<T>(); 89 } 90 }