Asp.NetCore 读取配置文件帮助类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | /// <summary> /// 读取配置文件信息 /// </summary> public class ConfigExtensions { public static IConfiguration Configuration { get ; set ; } static ConfigExtensions() { Configuration = new ConfigurationBuilder() .Add( new JsonConfigurationSource { Path = "appsettings.json" , ReloadOnChange = true }) .Build(); } /// <summary> /// 获得配置文件的对象值 /// </summary> /// <param name="jsonPath"></param> /// <param name="key"></param> /// <returns></returns> public static string GetJson( string jsonPath, string key) { if ( string .IsNullOrEmpty(jsonPath) || string .IsNullOrEmpty(key)) return null ; IConfiguration config = new ConfigurationBuilder().AddJsonFile(jsonPath).Build(); //json文件地址 return config.GetSection(key).Value; //json某个对象 } /// <summary> /// 根据配置文件和Key获得对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fileName">文件名称</param> /// <param name="key">节点Key</param> /// <returns></returns> public static T GetAppSettings<T>( string fileName, string key) where T : class , new () { if ( string .IsNullOrEmpty(fileName) || string .IsNullOrEmpty(key)) return null ; var baseDir = AppContext.BaseDirectory + "json/" ; var currentClassDir = baseDir; IConfiguration config = new ConfigurationBuilder() .SetBasePath(currentClassDir) .Add( new JsonConfigurationSource { Path = fileName, Optional = false , ReloadOnChange = true }).Build(); var appconfig = new ServiceCollection().AddOptions() .Configure<T>(config.GetSection(key)) .BuildServiceProvider() .GetService<IOptions<T>>() .Value; return appconfig; } /// <summary> /// 获取自定义配置文件配置 /// </summary> /// <typeparam name="T">配置模型</typeparam> /// <param name="key">根节点</param> /// <param name="configPath">配置文件名称</param> /// <returns></returns> public T GetAppSettingss<T>( string key, string configPath) where T : class , new () { if ( string .IsNullOrEmpty(key) || string .IsNullOrEmpty(configPath)) return null ; IConfiguration config = new ConfigurationBuilder().Add ( new JsonConfigurationSource { Path = configPath, ReloadOnChange = true }).Build(); var appconfig = new ServiceCollection() .AddOptions() .Configure<T>(config.GetSection(key)) .BuildServiceProvider() .GetService<IOptions<T>>() .Value; return appconfig; } /// <summary> /// 获取自定义配置文件配置(异步方式) /// </summary> /// <typeparam name="T">配置模型</typeparam> /// <param name="key">根节点</param> /// <param name="configPath">配置文件名称</param> /// <returns></returns> public async Task<T> GetAppSettingsAsync<T>( string key, string configPath) where T : class , new () { if ( string .IsNullOrEmpty(key) || string .IsNullOrEmpty(configPath)) return null ; IConfiguration config = new ConfigurationBuilder() .Add( new JsonConfigurationSource { Path = configPath, ReloadOnChange = true }).Build(); var appconfig = new ServiceCollection() .AddOptions() .Configure<T>(config.GetSection(key)) .BuildServiceProvider() .GetService<IOptions<T>>() .Value; return await Task.Run(() => appconfig); } /// <summary> /// 获取自定义配置文件配置 /// </summary> /// <typeparam name="T">配置模型</typeparam> /// <param name="key">根节点</param> /// <param name="configPath">配置文件名称</param> /// <returns></returns> public List<T> GetListAppSettings<T>( string key, string configPath) where T : class , new () { if ( string .IsNullOrEmpty(key) || string .IsNullOrEmpty(configPath)) return null ; IConfiguration config = new ConfigurationBuilder() .Add( new JsonConfigurationSource { Path = configPath, ReloadOnChange = true }).Build(); var appconfig = new ServiceCollection() .AddOptions() .Configure<List<T>>(config.GetSection(key)) .BuildServiceProvider() .GetService<IOptions<List<T>>>() .Value; return appconfig; } /// <summary> /// 获取自定义配置文件配置(异步方式) /// </summary> /// <typeparam name="T">配置模型</typeparam> /// <param name="key">根节点</param> /// <param name="configPath">配置文件名称</param> /// <returns></returns> public async Task<List<T>> GetListAppSettingsAsync<T>( string key, string configPath) where T : class , new () { if ( string .IsNullOrEmpty(key) || string .IsNullOrEmpty(configPath)) return null ; IConfiguration config = new ConfigurationBuilder() .Add( new JsonConfigurationSource { Path = configPath, ReloadOnChange = true }) .Build(); var appconfig = new ServiceCollection() .AddOptions() .Configure<List<T>>(config.GetSection(key)) .BuildServiceProvider() .GetService<IOptions<List<T>>>() .Value; return await Task.Run(() => appconfig); } } |
分类:
C#.Net & MVC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架