NetCore 操作配置文件
默认配置 appsettings.json
环境配置 launchSettings.json,即开发时运行配置
添加依赖
1 2 3 | Install-Package Microsoft.Extensions.Configuration Install-Package Microsoft.Extensions.Configuration.Binder Install-Package Microsoft.Extensions.Configuration.Json |
获取默认配置
1 | string sqlConn = builder.Configuration.GetConnectionString( "Default" ) |
如:配置数据库连接字符串
1 2 3 | builder.Services.AddDbContext<Service.DataContext>(opt => { opt.UseSqlServer(builder.Configuration.GetConnectionString( "Default" )); }); |
添加自定义JSON配置文件
1 2 3 4 5 | builder.Host.ConfigureAppConfiguration((hostingContext, config) => { config.Sources.Clear(); config.AddJsonFile( "redis.json" , optional: false , reloadOnChange: true ); }); |
配置文件帮助类
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 | /// <summary> /// 读取配置文件帮助类 /// 注册服务: builder.Services.AddSingleton(new AppSettings()); /// 使用示例: /// string data = AppSettings.ReadString(key); /// DataConfig dataConfig = AppSettings.ReadObject<DataConfig>(key); /// </summary> public class AppSettings { #region 注入参数 /// <summary> /// 配置文件 /// </summary> private static IConfiguration? _config; #endregion #region 构造函数 /// <summary> /// 构造函数 /// </summary> /// <param name="configuration">配置文件</param> public AppSettings() { _config = new ConfigurationBuilder() .AddJsonFile( "appsettings.json" , true , reloadOnChange: true ) .Build(); } #endregion #region 静态方法 /// <summary> /// 读取指定节点信息 /// </summary> /// <param name="sessions">节点名称</param> /// <returns></returns> public static string ReadString( params string [] sessions) { try { if (_config != null && sessions.Any()) { string ? str = _config[ string .Join( ":" , sessions)]; if (! string .IsNullOrEmpty(str)) { return str; } } } catch { return string .Empty; } return string .Empty; } /// <summary> /// 读取实体信息 /// </summary> /// <param name="sessions">节点名称</param> /// <returns></returns> public static T ReadObject<T>( params string [] sessions) where T : class , new () { T data = new (); try { if (_config != null && sessions.Any()) { _config.Bind( string .Join( ":" , sessions), data); } } catch { return data; } return data; } /// <summary> /// 读取实体数组信息 /// </summary> /// <param name="sessions">节点名称</param> /// <returns></returns> public static List<T> ReadList<T>( params string [] sessions) where T : class { List<T> list = new (); try { if (_config != null && sessions.Any()) { _config.Bind( string .Join( ":" , sessions), list); } } catch { return list; } return list; } #endregion } |
注册服务
1 2 | // 配置文件,放在第一句,并注册为Singleton builder.Services.AddSingleton( new AppSettings()); |
使用示例
1 2 | string data = AppSettings.ReadString( "key" ); DataConfig dataConfig = AppSettings.ReadObject<DataConfig>( "key" ); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?