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