国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

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

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

    前言

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

    Json配置文件的使用

    在創建ASP.NET Core的項目的時候,框架會自動添加appsettings.json文件和添加IConfiguration的注入。

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

    當我們在Startup構造函數添加一個IConfiguration參數,框架就會根據注入庫來進行注入,除此之外還有IHostingEnvironment,如果在構造函數添加這個參數,框架也會注入對應的實現類

    如果我們想要自己添加Json配置,該怎么做呢?

        //SetBasePath方法用來指定配置文件的所在地,env.ContentRootPath是獲取或設置包含應用程序內容文件的目錄的絕對路徑。    //AddJsonFile方法是使用JsonConfigurationSource來接收Json文件,并添加到ConfigurationBuilder中的Sources中    //Build()調用       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}");}

    那復雜的鍵值或者數組,又該如何獲得呢?

    {    "Teacher": {    "name": "Tom",    "age": "12",    "Students": [      {"name": "Docker","age": "13"      },      {"name": "Nginx","age": "45"      }    ]  }    	}

    我們想要獲取Teacher的name值數組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這兩個中可以尋找規律,當然獲取方式不止這一種,還可以使用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();    //創建一個自帶的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添加代碼,通過構造函數來構造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配置文件的使用

    這里簡單記錄一下,提取配置文件的值大致與上面做法沒有太大的區別,在構造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}");

    到此這篇關于如何使用ASP.NET Core 配置文件的文章就介紹到這了,更多相關ASP.NET Core 配置文件內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

    標簽: ASP.NET
    主站蜘蛛池模板: 日本三级香港三级人妇99视 | 日韩一级免费毛片 | 香蕉视频国产精品 | 欧美日韩在线观看精品 | 特色毛片 | 国产成人精品福利网站在线观看 | 久久99久久精品久久久久久 | 亚洲国产天堂久久九九九 | 99爱视频在线观看 | 午夜性刺激免费视频观看不卡专区 | 久久精品国产一区二区 | 亚洲欧美片| 姐姐真漂亮在线视频中文版 | 精品一久久香蕉国产线看观 | 2018久久久国产精品 | 亚洲国产成人在线 | 亚洲国产欧洲综合997久久 | 看久久久久毛片婷婷色 | 美女与男人对肌免费网站 | 日本一区三区二区三区四区 | 特黄特黄一级高清免费大片 | 特别福利视频在线观看 | 亚洲精品一区二区久久 | 99久久精品国产免看国产一区 | 国产色视频在线观看免费 | 欧美成人看片一区二区三区尤物 | 页面升级亚洲 | 国内精品一区二区三区最新 | 毛片在线播放网站 | 日本免费二区三区久久 | 久久网视频 | 亚洲国产日韩a在线亚洲 | 国产精品久久久久国产精品三级 | 午夜影院0606 | 精品国产一区二区三区国产馆 | 亚洲视频在线播放 | 偷拍自拍第一页 | 精品视频一区二区三区四区 | 免费又黄又爽又猛大片午夜 | 国产日韩欧美自拍 | 私人毛片免费高清影视院丶 |