配置文件如下
{ "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>
显示效果