.NET 6中读取配置文件内容
NET Core 6.0之读取配置文件
在ASP.NET Core 6.0中,默认配置文件是appsettings.json,该文件存储的内容为JSON格式的字符串,我们一般都将程序的配置放在这个文件里面,提供给程序使用,那么我们该如何操作呢? |
ASP.NET Core默认加载顺序是appsettings.json->appsettings.Environment.json,它会根据当前的运行环境去加载不同的配置文件,最后appsettings.Environment.json 值将替代 appsettings.json 中的值,如果没有多个值,则取默认值。
在开始之前,我们先在appsettings.json中新增一些配置信息
"Wechat": { "AppId": "wx26c607c55f31745e", "AppSecret": "e7da82499266ca3fdf85290f68f8fd3a" }
一、简单读取配置
1、在Program.cs中尝试读取配置文件中AppId和AppSecret的值,可以用WebApplicationBuilder里面的Configuration属性来读取,取配置内容的方式有很多,比如:
string appId = builder.Configuration.GetSection("Wechat")["AppId"]; string appSecret = builder.Configuration.GetSection("Wechat")["AppSecret"];
或者
string appId1 = builder.Configuration["Wechat:AppId"]; string appSecret1 = builder.Configuration["Wechat:AppSecret"];
或者更深的层级取消
string appId1 =builder.Configuration["AppConfig:Wechat:Ap"]
string appSecret1 =builder.Configuration["AppConfig:Wechat:AppSecret"]
2、在非Program.cs里面读取配置,则需要注入IConfiguration实例,其他操作方式便和前面的一致,新建一个Controller
[Route("api/[controller]")] [ApiController] public class ConfigurationController : ControllerBase { private readonly IConfiguration Configuration; public ConfigurationController(IConfiguration configuration) { Configuration = configuration; } [HttpGet] public string ReadConfig() { return Configuration["Wechat:AppId"]; } }
直接访问api/Configuration,便能返回配置文件中的AppId信息,
3、配置绑定到实体
如果配置文件比较复杂,用前面的方式一个个的去取值,确实有些繁琐,所以,需要更高端的操作,直接把配置内容装载到实体类中,新建一个名为WechatConfiguration的类,里面加入与配置文件对应的属性
public class WechatConfiguration { public const string KEY = "Wechat"; public string AppId { get; set; } = String.Empty; public string AppSecret { get; set; } = String.Empty; }
这里,需要使用的IConfiguration的GetSection方法来获取指定节点的内容,然后使用Get将内容序列化为对象,看例子
Configuration.GetSection(WechatConfiguration.KEY).Get<WechatConfiguration>();
除了,使用Get取值,还可使用Bind方法,将值绑定到对象上
WechatConfiguration wechatConfiguration = new WechatConfiguration(); Configuration.GetSection(WechatConfiguration.KEY).Bind(wechatConfiguration);
这两种方式都能获取到配置文件修改后的内容,除了上面两种方式获取配置内容外,还可以使用直接将配置对象注入到容器中,
builder.Services.Configure(builder.Configuration.GetSection(WechatConfiguration.KEY));
然后在需要使用的类中注入IOptions,通过其Value属性来获取配置类对象
public class ConfigurationController : ControllerBase { private readonly IConfiguration Configuration; private readonly WechatConfiguration wechat; public ConfigurationController(IConfiguration configuration, IOptions options) { Configuration = configuration; wechat = options.Value; } }
如果配置类过多,那么在Program.cs便会显得杂乱臃肿,所以,可以将其移动到扩展方法以注册服务,这里新建一个扩展类
ConfigServiceCollectionExtensions,将前面的代码移动到该类中
public static class ConfigServiceCollectionExtensions { public static IServiceCollection AddConfig(this IServiceCollection services, IConfiguration config) { services.Configure(config.GetSection(WechatConfiguration.KEY)); return services; } }
然后在Program.cs中添加引用即可
builder.Services.AddConfig(builder.Configuration);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)