如何使用ASP.NET?Core?配置文件
前言
在ASP.NET ,我們使用XML格式的.Config文件來作為配置文件,而在ASP.NET Core,我們有了更多的選擇,可以用回XML,也可以用Json、Ini文件作為配置文件
Json配置文件的使用
在創(chuàng)建ASP.NET Core的項目的時候,框架會自動添加appsettings.json文件和添加IConfiguration的注入。
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}當(dāng)我們在Startup構(gòu)造函數(shù)添加一個IConfiguration參數(shù),框架就會根據(jù)注入庫來進(jìn)行注入,除此之外還有IHostingEnvironment,如果在構(gòu)造函數(shù)添加這個參數(shù),框架也會注入對應(yīng)的實現(xiàn)類
如果我們想要自己添加Json配置,該怎么做呢?
//SetBasePath方法用來指定配置文件的所在地,env.ContentRootPath是獲取或設(shè)置包含應(yīng)用程序內(nèi)容文件的目錄的絕對路徑。
//AddJsonFile方法是使用JsonConfigurationSource來接收J(rèn)son文件,并添加到ConfigurationBuilder中的Sources中
//Build()調(diào)用
var config=new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.Build();
Configuration = config;
如果不通過IHostingEnvironment來獲取絕對路徑,也可以使用Directory.GetCurrentDirectory()方法來獲得
測試:
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第二個的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這兩個中可以尋找規(guī)律,當(dāng)然獲取方式不止這一種,還可以使用Config[“Teacher:Students:1:name”]來獲取
如果我們想用對象來存儲配置文件的鍵值該如何做呢?
//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)建一個自帶的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添加代碼,通過構(gòu)造函數(shù)來構(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配置文件的使用
這里簡單記錄一下,提取配置文件的值大致與上面做法沒有太大的區(qū)別,在構(gòu)造IConfiguration的時候把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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET WebAPI2復(fù)雜請求跨域設(shè)置的方法介紹
這篇文章主要給大家介紹了關(guān)于ASP.NET WebAPI2復(fù)雜請求跨域設(shè)置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用ASP.NET具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
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>】實現(xiàn)原理
本文主要介紹了.NET中可空值類型的實現(xiàn)原理,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
asp.net DataTable導(dǎo)出Excel自定義列名的方法
本文分享了asp.net DataTable導(dǎo)出Excel 自定義列名的具體實現(xiàn)方法,步驟清晰,代碼詳細(xì),需要的朋友可以參考借鑒,下面就跟小編一起來看看吧2016-12-12
.Net Core WebApi的簡單創(chuàng)建以及使用方法
這篇文章主要給大家介紹了關(guān)于.Net Core WebApi的簡單創(chuàng)建以及使用方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用.Net Core WebApi具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
詳解如何在ASP.Net Core中實現(xiàn)健康檢查
這篇文章主要介紹了詳解如何在ASP.Net Core中實現(xiàn)健康檢查,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

