首页 > 移动平台 > 详细

.net core读取appsettings.json的配置

时间:2021-06-12 01:17:13      阅读:17      评论:0      收藏:0      [点我收藏+]

假设存在这样的json配置文件

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*","Level": {
    "A": "Good",
    "Other": [
      "B","C","D"
    ]
  }
}

想分别读取配置

在控制器中:

        private readonly IConfiguration _config;

        public HomeController(IConfiguration config)
        {
            _config = config;
        }

 

            #region 读取配置
            Console.WriteLine(_config["AllowedHosts"]);  //一级
            Console.WriteLine(_config["Logging:LogLevel:Default"]);  //多级单个
            Console.WriteLine(_config["Level:Other:0"]);  //多级多个
            var levels = _config.GetSection("Level").GetSection("Other").GetChildren().Select(v => v.Value).ToArray(); //全部
            foreach (var item in levels)
            {
                Console.WriteLine(item);
            }
            #endregion

 

.net core读取appsettings.json的配置

原文:https://www.cnblogs.com/AduBlog/p/14877046.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!