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

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

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

瀏覽:59日期: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
    主站蜘蛛池模板: 国内精品久久久久影院亚洲 | 国产精品夜色视频一级区 | 9cao视频精品 | 九九国产视频 | 18视频免费网站 | 91最新网站 | 一及黄色| 日本a级片免费看 | 日韩制服诱惑 | 久久久久依人综合影院 | 极品美女户外勾搭无套 | 久久精品国产亚洲 | 亚洲成在人线免费视频 | 全免费a级毛片免费看不卡 全免费毛片在线播放 | 中国japanesevideo乱 | 亚洲高清在线观看 | 亚洲欧美国产一区二区三区 | 免费99热在线观看 | 粉嫩高中生的第一次在线观看 | 欧美a区| 加勒比heyzo| 在线欧美国产 | 亚洲深夜| 九九在线精品视频xxx | 久久精品免费观看国产软件 | 国产91成人 | 久久久久久色 | 日本 国产 欧美 | 精品亚洲成a人片在线观看 精品亚洲成a人在线播放 | 日韩一级黄色毛片 | 成年人黄色片 | 亚洲视频一区在线观看 | 国产视频久 | 日韩视频一区二区 | 免费一级做a爰片性色毛片 免费一极毛片 | 国产精品不卡在线 | 亚洲精品综合久久中文字幕 | 操美国女人 | 91 久久 | 亚洲国产精品久久网午夜 | 在线观看一二三区 |