如何使用ASP.NET?Core?配置文件
前言
在ASP.NET ,我們使用XML格式的.Config文件來(lái)作為配置文件,而在ASP.NET Core,我們有了更多的選擇,可以用回XML,也可以用Json、Ini文件作為配置文件
Json配置文件的使用
在創(chuàng)建ASP.NET Core的項(xiàng)目的時(shí)候,框架會(huì)自動(dòng)添加appsettings.json文件和添加IConfiguration的注入。
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}當(dāng)我們?cè)赟tartup構(gòu)造函數(shù)添加一個(gè)IConfiguration參數(shù),框架就會(huì)根據(jù)注入庫(kù)來(lái)進(jìn)行注入,除此之外還有IHostingEnvironment,如果在構(gòu)造函數(shù)添加這個(gè)參數(shù),框架也會(huì)注入對(duì)應(yīng)的實(shí)現(xiàn)類
如果我們想要自己添加Json配置,該怎么做呢?
//SetBasePath方法用來(lái)指定配置文件的所在地,env.ContentRootPath是獲取或設(shè)置包含應(yīng)用程序內(nèi)容文件的目錄的絕對(duì)路徑。
//AddJsonFile方法是使用JsonConfigurationSource來(lái)接收J(rèn)son文件,并添加到ConfigurationBuilder中的Sources中
//Build()調(diào)用
var config=new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.Build();
Configuration = config;
如果不通過(guò)IHostingEnvironment來(lái)獲取絕對(duì)路徑,也可以使用Directory.GetCurrentDirectory()方法來(lái)獲得
測(cè)試:
public IActionResult Index()
{
var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build();
string value = config.GetConnectionString("MySqlConnection");
string value2 = config.GetSection("Test").Value;
return Content($"{value},Test:{value2}");
}public IActionResult Index()
{
var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build();
string value = config.GetConnectionString("MySqlConnection");
string value2 = config.GetSection("Test").Value;
return Content($"{value},Test:{value2}");
}
那復(fù)雜的鍵值或者數(shù)組,又該如何獲得呢?
{
"Teacher": {
"name": "Tom",
"age": "12",
"Students": [
{
"name": "Docker",
"age": "13"
},
{
"name": "Nginx",
"age": "45"
}
]
}
}我們想要獲取Teacher的name值和數(shù)組Students第二個(gè)的name值,怎么獲取呢?
public IActionResult Index()
{
var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build();
string value = config.GetSection("Teacher:name").Value;
//
string value2 = config.GetSection("Teacher:Students:1:name").Value;
return Content($"{value},Test:{value2}");
}
PS:從Teacher:name和Teacher:Students:1:name這兩個(gè)中可以尋找規(guī)律,當(dāng)然獲取方式不止這一種,還可以使用Config[“Teacher:Students:1:name”]來(lái)獲取
如果我們想用對(duì)象來(lái)存儲(chǔ)配置文件的鍵值該如何做呢?
//appsetting.json
{
"RedisConfig": {
"host": "127.0.0.1",
"MasterPort": "6379",
"SlavePort": "6380",
"PassWord": "wen123"
}
}RedisHelper類
public class RedisHelper:IRedis
{
public string host { get; set; }
public string MasterPort { get; set; }
public string SlavePort { get; set; }
public string PassWord { get; set; }
}public IActionResult Index()
{
var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build();
//創(chuàng)建一個(gè)自帶的IOC容器
var collection = new ServiceCollection();
collection.AddOptions().Configure<RedisHelper>(config.GetSection("RedisConfig"));
RedisHelper redishelper = collection.BuildServiceProvider().GetService<IOptions<RedisHelper>>().Value;
return Content($"host:{redishelper.host},MasterPort:{redishelper.MasterPort}");
}
還有另一種寫法:在Startup類的ConfigureServices方法里面,向services添加代碼,通過(guò)構(gòu)造函數(shù)來(lái)構(gòu)造RedisHelper類
services.AddOptions().Configure<RedisHelper>(Configuration.GetSection("RedisConfig"));private RedisHelper _redis;
public HomeController(IOptions<RedisHelper> options)
{
_redis = options.Value;
}
public IActionResult Index()
{
return Content($"host:{_redis.host},MasterPort:{_redis.MasterPort}");
}
XML配置文件的使用
這里簡(jiǎn)單記錄一下,提取配置文件的值大致與上面做法沒(méi)有太大的區(qū)別,在構(gòu)造IConfiguration的時(shí)候把AddJsonFile改成AddXmlFile就行了
//XMLDemo文件
<?xml version="1.0" encoding="utf-8" ?>
<Test>
<mysqlConnectionStrings>sdfl</mysqlConnectionStrings>
<test>
<connection>sdfasdf</connection>
<connection2>sdfdsafsfs</connection2>
</test>
<test2>
<test3>
<connection>dfgfdg</connection>
</test3>
</test2>
</Test>public IActionResult Index()
{
var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddXmlFile("XMLDemo.xml").Build();
var value = config.GetSection("mysqlConnectionStrings").Value;
var value2 = config.GetSection("test:connection2").Value;
return Content($"value:{value},value2:{value2}");
到此這篇關(guān)于如何使用ASP.NET Core 配置文件的文章就介紹到這了,更多相關(guān)ASP.NET Core 配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET WebAPI2復(fù)雜請(qǐng)求跨域設(shè)置的方法介紹
這篇文章主要給大家介紹了關(guān)于ASP.NET WebAPI2復(fù)雜請(qǐng)求跨域設(shè)置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用ASP.NET具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
ASP.NET中操作數(shù)據(jù)庫(kù)的基本步驟分享
ASP.NET中操作數(shù)據(jù)庫(kù)的基本步驟分享,學(xué)習(xí)asp.net的朋友可以參考下。2011-10-10
Asp.Net Core 調(diào)用第三方Open API查詢物流數(shù)據(jù)的示例
這篇文章主要介紹了Asp.Net Core 調(diào)用第三方Open API查詢物流數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)使用Asp.Net Core,感興趣的朋友可以了解下2021-03-03
.NET中可空值類型【Nullable<T>】實(shí)現(xiàn)原理
本文主要介紹了.NET中可空值類型的實(shí)現(xiàn)原理,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
.net 彈出消息框后導(dǎo)致頁(yè)面樣式變亂解決方法
點(diǎn)擊按鈕,執(zhí)行提交操作,彈出消息框后,頁(yè)面的樣式變亂,已經(jīng)確定了不是css樣式的問(wèn)題,接下來(lái)與大家共同探討下究竟是什么問(wèn)題導(dǎo)致頁(yè)面變亂2013-04-04
asp.net DataTable導(dǎo)出Excel自定義列名的方法
本文分享了asp.net DataTable導(dǎo)出Excel 自定義列名的具體實(shí)現(xiàn)方法,步驟清晰,代碼詳細(xì),需要的朋友可以參考借鑒,下面就跟小編一起來(lái)看看吧2016-12-12
.Net Core WebApi的簡(jiǎn)單創(chuàng)建以及使用方法
這篇文章主要給大家介紹了關(guān)于.Net Core WebApi的簡(jiǎn)單創(chuàng)建以及使用方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.Net Core WebApi具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
詳解如何在ASP.Net Core中實(shí)現(xiàn)健康檢查
這篇文章主要介紹了詳解如何在ASP.Net Core中實(shí)現(xiàn)健康檢查,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

