当前位置: 代码网 > it编程>编程语言>Asp.net > ASP.NET Core读取配置文件

ASP.NET Core读取配置文件

2024年05月18日 Asp.net 我要评论
asp.net core 中,可以使用 configurationbuilder 对象来构建。主要分为三部:配置数据源 -> configurationbuilder -> 使用。数据源可

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读取配置文件的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com