阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

.NET Core 6.0之读取配置文件

34次阅读
没有评论

共计 2751 个字符,预计需要花费 7 分钟才能阅读完成。

导读 在 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"
  }

.NET Core 6.0 之读取配置文件

简单读取配置

现在我们就尝试读取配置文件中 AppId 和 AppSecret 的值,在 Program.cs 中,我们直接可以用 WebApplicationBuilder 里面的 Configuration 属性来读取, 取配置内容的方式有很多,比如:

string appId = builder.Configuration.GetSection("Wechat")["AppId"];
string appSecret = builder.Configuration.GetSection("Wechat")["AppSecret"];

.NET Core 6.0 之读取配置文件

还可以这样

string appId1 = builder.Configuration["Wechat:AppId"];
string appSecret1 = builder.Configuration["Wechat:AppSecret"];

当然,它还可以更深的层级,如:builder.Configuration[“AppConfig:Wechat:AppSecret”]

.NET Core 6.0 之读取配置文件

如果我们想在非 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 信息,

.NET Core 6.0 之读取配置文件

配置绑定到实体

如果配置文件比较复杂,我们依然用前面的方式一个个的去取值,那确实有些繁琐,所以,我们需要更高端的操作,直接把配置内容装载到实体类中,这我新建一个名为 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();

.NET Core 6.0 之读取配置文件
.NET Core 6.0 之读取配置文件

除了使用 Get 取值, 还可使用 Bind 方法,将值绑定到对象上

WechatConfiguration wechatConfiguration = new WechatConfiguration();
Configuration.GetSection(WechatConfiguration.KEY).Bind(wechatConfiguration);

.NET Core 6.0 之读取配置文件

这两种方式都能获取到配置文件修改后的内容,除了上面两种方式获取配置内容外,还可以使用直接将配置对象注入到容器中,

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;
    }
}

.NET Core 6.0 之读取配置文件
如果配置类过多,那么在 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);

配置文件读取就先了解这么,明天就要开始上班了,又要忙起来了,后面有时间再继续学习 ASP.NET Core 中的路由。

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计2751字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中