.net core3.1 读取配置文件

2021-04-11 00:27

阅读:403

标签: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 _logger;
        private readonly IConfiguration _iConfiguration;
        public FirstController(ILogger 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


评论


亲,登录后才可以留言!