ASP.NETCore读取配置文件

ASP.NETCore读取配置文件

ASP.NET Core 中,可以使用 ConfigurationBuilder 对象来构建。

主要分为三部:配置数据源 -> ConfigurationBuilder -> 使用。

数据源可来自字典或配置文件。

数据源要么继承 IConfigurationSource ,要么从配置文件中读取。

1,来自字典

我们先使用字典存储键值对,来设置配置, test = 配置,然后使用 ConfigurationBuilder.Add() 方法添加数据源, Add 方法可以添加继承了 IConfigurationSource的数据源。

MemoryConfigurationSource 继承了 IConfigurationSource ,使用字典作为数据源。

var dic = new Dictionary<string, string>() { ["test"] = "配置" }; var config = new ConfigurationBuilder() .Add(new MemoryConfigurationSource() { InitialData = dic }).Build(); string test = config["test"];

老是 new 不太爽,可以使用下面的方法来读取字典中的数据源:

var dic = new Dictionary<string, string>() { ["test"] = "配置" }; var config = new ConfigurationBuilder() .AddInMemoryCollection(dic) .Build(); string test = config["test"]; 2,来自配置文件

假如在 项目根目录下创建一个 json 文件,内容如下:

{ "test":"配置" }

那么可以这样读取配置:

var config = new ConfigurationBuilder() .AddJsonFile("test.json") .Build(); string test = config["test"]; Console.WriteLine(test);

如果配置文件不在根目录下,则可以使用 SetBasePath() 来定义路径,示例如下:

var config = new ConfigurationBuilder() .SetBasePath("E:\\test\\aaa") .AddJsonFile("test.json") .Build();

上面看到,获取配置项是非常简单的, config["{KeyName}"] 即可获得 value

另外,可以监控 json 文件,当 json 文件发生更改时,主动更新。

config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); 3,层次结构

配置数据源可以有层次结构。

ASP.NET Core 中,都会有个 appsettings.json 文件,其内容如下:

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } } }

那么我们使用时,可以使用 : 符号获取下一层子项的配置。

var config = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); string test = config["Logging:LogLevel:Default"];

如果你只想 获取 json 文件中 LogLevel 部分的配置,可以使用 GetSection() 方法。

var config = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build() .GetSection("Logging:LogLevel"); string test = config["Default"];

通过 json 配置文件,我们可以很方便地构建层级结构的配置,如果想在字典中存储,可以使用 "{k1}:{k2}" 这种形式存。例如:

var dic = new Dictionary<string, string>() { ["testParent:Child1"] = "6", ["testParent:Child2"] = "666" }; var config = new ConfigurationBuilder() .AddInMemoryCollection(dic) .Build().GetSection("testParent"); string test = config["Child1"]; 4,映射

我们可以使用 Get<>() 方法,将配置映射为对象。

public class TestOptions { public string A { get; set; } public string B { get; set; } public string C { get; set; } } var dic = new Dictionary<string, string>() { ["A"] = "6", ["B"] = "66", ["C"] = "666" }; TestOptions config = new ConfigurationBuilder() .AddInMemoryCollection(dic) .Build().Get<TestOptions>();

到此这篇关于ASP.NET Core读取配置文件的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持易知道(ezd.cc)。

推荐阅读

    学习写字楼新选择6000元主流配置

    学习写字楼新选择6000元主流配置,,这种配置需要考虑双核心的办公和娱乐平台,充分考虑办公室的办公需求和娱乐需求,以约6000元的预算和cost-e

    酷睿I7 配置

    酷睿I7 配置,配置,玩家国度啦华硕 Rampage II Extreme(3800元)如果米不够,也可以把Extreme改为Gene,不过是小板内存推荐金士顿6G DDR3 2000骇

    提高3A四核羿龙II游戏配置的性能

    提高3A四核羿龙II游戏配置的性能,,以节能环保为主题的IT产业,目前3A低端平台处理器、主板芯片组、独立开发卡性能突出,特别是在与AMD的处理

    opporeno8参数配置及价格

    opporeno8参数配置及价格,面部,亿元,Oppo的荣誉2020年1月4日,接近屏幕关闭传感器是否支持双卡:支持oppor11splus什么时候上市的Oppo R11S P

    查看配置:酷睿i3530集展示办公平台

    查看配置:酷睿i3530集展示办公平台,,由于时间和精力的关系,我们不可能对所有的配置进行评论,希望我们能理解,我希望我们的评论能在那些需要帮

    3500元超额值学生娱乐结构的优化配置

    3500元超额值学生娱乐结构的优化配置,,作为一个DIY的主流用户领域的学生,每个用户51学生攒机的高峰。因为学生用户没有稳定的收入来源,攒机

    电脑配置快捷键|查看电脑配置快捷键

    电脑配置快捷键|查看电脑配置快捷键,,查看电脑配置快捷键1.在win10中,快捷键组合win+E已经变成了“快速访问”,不再是以前的“我的电脑了”2

    6000元教你黑盒5000+权限配置

    6000元教你黑盒5000+权限配置,,一系列的价格后,最经典的AMD双核处理器mdash;mdash;Athlon 64 X2 5000 +黑版mdash;mdash;目前的价格已经低至565