如何使用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中g(shù)ridview控件的多種使用方法(上)
這篇文章向大家推薦如何靈活掌握asp.net中g(shù)ridview控件的多種使用方法,感興趣的小伙伴們可以參考一下2015-11-11
VS2012實(shí)現(xiàn)簡(jiǎn)單登錄界面
這篇文章主要為大家詳細(xì)介紹了VS2012實(shí)現(xiàn)簡(jiǎn)單登錄界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
PHP session實(shí)現(xiàn)購(gòu)物車功能
這篇文章主要為大家詳細(xì)介紹了PHP session實(shí)現(xiàn)購(gòu)物車功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
ASP.NET Core 3框架揭秘之 異步線程無(wú)法使用IServiceProvider問(wèn)題
這篇文章主要介紹了ASP.NET Core 3框架揭秘之異步線程無(wú)法使用IServiceProvider問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
ASP.NET實(shí)現(xiàn)多域名多網(wǎng)站共享Session值的方法
實(shí)現(xiàn)功能:可設(shè)置哪些站點(diǎn)可以共享Session值,這樣就防止別人利用這個(gè)去訪問(wèn),要想實(shí)現(xiàn)這個(gè)功能就必須得把Session值 放入數(shù)據(jù)庫(kù)中, 所有我們先在VS命令工具下注冊(cè)一個(gè)2011-11-11
asp.net中利用Jquery+Ajax+Json實(shí)現(xiàn)無(wú)刷新分頁(yè)的實(shí)例代碼
本篇文章主要是對(duì)asp.net中利用Jquery+Ajax+Json實(shí)現(xiàn)無(wú)刷新分頁(yè)的實(shí)例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,需要對(duì)大家有所幫助2014-02-02

