午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

如何使用ASP.NET Core 配置文件

瀏覽:285日期:2022-06-08 16:58:58
目錄
  • 前言
  • Json配置文件的使用
    • RedisHelper類
  • XML配置文件的使用

    前言

    在ASP.NET ,我們使用XML格式的.Config文件來作為配置文件,而在ASP.NET Core,我們有了更多的選擇,可以用回XML,也可以用Json、Ini文件作為配置文件

    Json配置文件的使用

    在創(chuàng)建ASP.NET Core的項(xiàng)目的時(shí)候,框架會自動(dòng)添加appsettings.json文件和添加IConfiguration的注入。

    public Startup(IConfiguration configuration){       Configuration = configuration;}

    當(dāng)我們在Startup構(gòu)造函數(shù)添加一個(gè)IConfiguration參數(shù),框架就會根據(jù)注入庫來進(jìn)行注入,除此之外還有IHostingEnvironment,如果在構(gòu)造函數(shù)添加這個(gè)參數(shù),框架也會注入對應(yīng)的實(shí)現(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第二個(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”]來獲取

    如果我們想用對象來存儲配置文件的鍵值該如何做呢?

    //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添加代碼,通過構(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的時(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)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

    標(biāo)簽: ASP.NET
    主站蜘蛛池模板: 黄色亚洲视频 | 国内成人自拍视频 | 欧美啪啪网 | 天天色一色 | 丰满女人性猛交 | 黄色一级视频在线观看 | 好吊妞视频一区二区三区 | 极品探花在线观看 | 处破女av一区二区 | 黄色一级免费网站 | 国产成人在线免费观看视频 | 毛片视频免费观看 | 玖玖成人 | 欧美九九九 | 国产精品mm| 法国极品成人h版 | 久久久久免费看 | 免费看av大片 | 午夜日韩视频 | 国产理论片在线观看 | 久久97| 亚洲欧美日韩在线播放 | 欧美精品网址 | 婷婷啪啪 | av网站播放 | 在线黄网 | 欧美成人自拍视频 | 成年人免费视频播放 | 天堂影院av | 日韩国产精品一区二区 | 成人一级黄色片 | 精品一区二区不卡 | 日本一区二区三区四区视频 | 91无套直看片红桃 | 17c国产精品一区二区 | 欧美成人二区 | 色综合中文 | 啪啪小视频网站 | 亚洲欧美另类日韩 | 色综合欧美 | 中文字幕在线2018 |