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處理HTTP請求的流程:IHttpModule、IHttpHandler與管道事件
這篇文章介紹了ASP.NET處理HTTP請求的流程:IHttpModule、IHttpHandler與管道事件,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
.Net結(jié)構(gòu)型設(shè)計模式之適配器模式(Adapter)
這篇文章介紹了.Net結(jié)構(gòu)型設(shè)計模式之適配器模式(Adapter),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
asp.net實例代碼protected override void Render(HtmlTextWriter wri
把最終要輸出的html壓縮后再輸出和最終輸出前先存為html文件,如果發(fā)布最新信息了,可以打開一次default.aspx,然后他又會生成一次html2008-08-08

