欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET Core讀取配置文件

 更新時(shí)間:2022年02月23日 08:58:55   作者:癡者工良  
這篇文章介紹了ASP.NET Core讀取配置文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

ASP.NET Core 中,可以使用 ConfigurationBuilder 對(duì)象來(lái)構(gòu)建。

主要分為三部:配置數(shù)據(jù)源 -> ConfigurationBuilder -> 使用。

數(shù)據(jù)源可來(lái)自字典或配置文件。

數(shù)據(jù)源要么繼承 IConfigurationSource ,要么從配置文件中讀取。

1,來(lái)自字典

我們先使用字典存儲(chǔ)鍵值對(duì),來(lái)設(shè)置配置, test = 配置,然后使用 ConfigurationBuilder.Add() 方法添加數(shù)據(jù)源, Add 方法可以添加繼承了 IConfigurationSource的數(shù)據(jù)源。

MemoryConfigurationSource 繼承了 IConfigurationSource ,使用字典作為數(shù)據(jù)源。

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

老是 new 不太爽,可以使用下面的方法來(lái)讀取字典中的數(shù)據(jù)源:

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

2,來(lái)自配置文件

假如在 項(xiàng)目根目錄下創(chuàng)建一個(gè) json 文件,內(nèi)容如下:

{
"test":"配置"
}

那么可以這樣讀取配置:

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

如果配置文件不在根目錄下,則可以使用 SetBasePath() 來(lái)定義路徑,示例如下:

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

上面看到,獲取配置項(xiàng)是非常簡(jiǎn)單的, config["{KeyName}"] 即可獲得 value。

另外,可以監(jiān)控 json 文件,當(dāng) json 文件發(fā)生更改時(shí),主動(dòng)更新。

                config.AddJsonFile("appsettings.json", 
                    optional: true, 
                    reloadOnChange: true);

3,層次結(jié)構(gòu)

配置數(shù)據(jù)源可以有層次結(jié)構(gòu)。

ASP.NET Core 中,都會(huì)有個(gè) appsettings.json 文件,其內(nèi)容如下:

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

那么我們使用時(shí),可以使用 : 符號(hào)獲取下一層子項(xiàng)的配置。

            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"];

通過(guò) json 配置文件,我們可以很方便地構(gòu)建層級(jí)結(jié)構(gòu)的配置,如果想在字典中存儲(chǔ),可以使用 "{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<>() 方法,將配置映射為對(duì)象。

    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>();

到此這篇關(guān)于ASP.NET Core讀取配置文件的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論