ASP.NET Core讀取配置文件
ASP.NET Core 中,可以使用 ConfigurationBuilder 對象來構(gòu)建。
主要分為三部:配置數(shù)據(jù)源 -> ConfigurationBuilder -> 使用。
數(shù)據(jù)源可來自字典或配置文件。
數(shù)據(jù)源要么繼承 IConfigurationSource ,要么從配置文件中讀取。
1,來自字典
我們先使用字典存儲鍵值對,來設(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 不太爽,可以使用下面的方法來讀取字典中的數(shù)據(jù)源:
var dic = new Dictionary<string, string>() { ["test"] = "配置" }; var config = new ConfigurationBuilder() .AddInMemoryCollection(dic) .Build(); string test = config["test"];
2,來自配置文件
假如在 項目根目錄下創(chuàng)建一個 json 文件,內(nèi)容如下:
{ "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
。
另外,可以監(jiān)控 json 文件,當(dāng) json 文件發(fā)生更改時,主動更新。
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
3,層次結(jié)構(gòu)
配置數(shù)據(jù)源可以有層次結(jié)構(gòu)。
ASP.NET Core 中,都會有個 appsettings.json 文件,其內(nèi)容如下:
{ "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 配置文件,我們可以很方便地構(gòu)建層級結(jié)構(gòu)的配置,如果想在字典中存儲,可以使用 "{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>();
到此這篇關(guān)于ASP.NET Core讀取配置文件的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET?Core中Razor頁面的Handlers處理方法詳解
本文詳細(xì)講解了ASP.NET?Core中Razor頁面的Handlers處理方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02ASP.net(c#)打造24小時天氣預(yù)報及實時天氣
ASP.net(c#)打造24小時天氣預(yù)報及實時天氣...2006-12-12.Net行為型設(shè)計模式之職責(zé)鏈模式(Chain of Responsibility)
這篇文章介紹了.Net行為型設(shè)計模式之職責(zé)鏈模式(Chain of Responsibility),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05詳解ABP框架的參數(shù)有效性驗證和權(quán)限驗證
ABP框架是基于ASP.NET的Web開發(fā)框架(GitHub: https://github.com/aspnetboilerplate),在ASP.NET框架之上又添加了更強大的功能,這里我們就來詳解ABP框架的參數(shù)有效性驗證和權(quán)限驗證2016-06-06asp.net下SQLite(輕量級最佳數(shù)據(jù)庫) 原理分析和開發(fā)應(yīng)用
SQLite是一個開源的嵌入式關(guān)系數(shù)據(jù)庫,它在2000年由D. Richard Hipp發(fā)布,它的減少應(yīng)用程序管理數(shù)據(jù)的開銷,SQLite可移植性好,很容易使用,很小,高效而且可靠2011-10-10微軟 Visual Studio 2010官方下載地址給大家
昨天VS2010在網(wǎng)上報道都已經(jīng)發(fā)布了,現(xiàn)在今天在網(wǎng)上找到Visual Studio 2010官方下載地址,提供給大家下載。2010-04-04