zoukankan      html  css  js  c++  java
  • Asp.Net.Core 5 配置文件读取

    配置文件如下

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "Version": "v1.0",
      "AllowedHosts": "*"
    }

    1,构造函数注入IConfiguration

    private readonly ILogger<HomeController> _logger;
            private readonly IConfiguration _Configuration;
    
            public HomeController(ILogger<HomeController> logger,
                IConfiguration configuration
                )
            {
                _logger = logger;
                _Configuration = configuration;
            }

    2,读取配置文件信息

      public IActionResult ReadConfig()
            {
                base.ViewBag.Version = _Configuration["Version"];
                base.ViewBag.AllowedHosts = _Configuration["AllowedHosts"];
                base.ViewBag.LoggingDefault = _Configuration["Logging:LogLevel:Default"];
                return View();
            }
    

    3,ReadConfig.cshtml 前端显示配置文件 --配置文件信息不给暴露

    <h1>配置文件读取</h1>
    <div>Version:<b>@ViewBag.Version</b></div>
    <div>AllowedHosts:<b>@ViewBag.AllowedHosts</b></div>
    <div>Logging-LogLevel-Default:<b>@ViewBag.LoggingDefault</b></div>

    显示效果

  • 相关阅读:
    python--异常处理
    Codeforces 1499D
    Codeforces 1263E
    Codeforces 1493D
    Codeforces 1492D
    Codeforces 1490G
    Codeforces 1487E
    Codeforces 1485D
    Codeforces 1485C
    P6917 [ICPC2016 WF]Balanced Diet
  • 原文地址:https://www.cnblogs.com/Linc2010/p/14315657.html
Copyright © 2011-2022 走看看