码迷,mamicode.com
首页 > Web开发 > 详细

.net core3.1 读取配置文件

时间:2020-03-05 15:29:01      阅读:584      评论:0      收藏:0      [点我收藏+]

标签:format   first   lin   return   info   readonly   conf   col   ado   

1.配置文件

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "wizards": [
    {
      "Name": "Gandalf",
      "Age": "1000"
    },
    {
      "Name": "Harry",
      "Age": "17"
    }
  ],
  "AllowedHosts": "*"
}

2.读取方法

  public class FirstController : Controller
    {
        private readonly ILogger<FirstController> _logger;
        private readonly IConfiguration _iConfiguration;
        public FirstController(ILogger<FirstController> logger, IConfiguration iConfiguration)
        {
            _logger = logger;
            _iConfiguration = iConfiguration;
        }
        public IActionResult Index()
        {
            string Urls = _iConfiguration["Urls"];
            string LogLevel = _iConfiguration["Logging:LogLevel:Default"];
            string Name = _iConfiguration["wizards:0:Name"];

            Console.WriteLine("Name:" + Name);
            Console.WriteLine("LogLevel:" + LogLevel);
            Console.WriteLine("Urls:" + Urls); //运行时的url
            _logger.LogWarning("测试日志");
            return View();
        }
    }

 

.net core3.1 读取配置文件

标签:format   first   lin   return   info   readonly   conf   col   ado   

原文地址:https://www.cnblogs.com/wangdash/p/12420122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!