如何在 ASP.NET5 中正确读取 config.json 中的嵌套配置值?

问题描述 投票:0回答:4

我正在关注 ASP.NET 5 的一些示例,但我对如何正确读取“嵌套”配置值(如果这是正确的术语)感到困惑。

这是

config.json

的相关部分:

{ "ApplicationName" : "OwNextApp", "AppSettings": { "SiteTitle": "OwNext" }, }

以及

HomeController.cs

的相关部分:

public IActionResult About() { var appNestedNameFailed = _config.Get("AppSettings.SiteTitle"); var appNestedNameSuccess = _config.Get("AppSettings:SiteTitle"); var appName = _config.Get("ApplicationName"); ViewBag.Message = string.Format(@"Your APP NAME: {0}; APP NESTED NAME FAILED: {1}; APP NESTED NAME SUCCESS: {2}", appName, appNestedNameFailed, appNestedNameSuccess); return View(); }

appNestedNameFailed

的值是空的(我在研究之前的初步尝试)。并且 
appNestedNameSuccess
 具有价值;经过研究并在
Configuration的测试中发现(显示相关代码):

// Assert Assert.Equal("IniValue1", config.Get("IniKey1")); Assert.Equal("IniValue2", config.Get("IniKey2:IniKey3"));

有人可以解释为什么会这样吗?为什么使用

:

 而不是 
.
 更有意义?从我与 JSON 数据的交互来看,通常 
.
 表示法工作得很好,例如
如何访问嵌套的 json 数据

此外,我发现了类似的

SO问题,但这并没有解释为什么选择:

c# asp.net asp.net-core config.json
4个回答
33
投票
这是我们第一次创建配置模型时决定的约定。我们从 json 开始,

:

 是那里的分隔符。

无论如何,如果您不想担心这些约定,我建议使用

ConfigurationBinder 它将配置绑定到模型(强类型对象)。 这里是 GitHub 上的测试,可以作为示例。


19
投票
using Microsoft.Extensions.Configuration; using System.IO; IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); var connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnection"); // or var connectionString2= configuration.GetSection("ConnectionStrings").GetSection("DefaultConnection").Value;

appsettings.json:

{ "ConnectionStrings": { "DefaultConnection": "myconnection" }, }
    

1
投票
深入了解

JsonConfigurationFileParser 源的内部,并归咎于查看的进入/退出方法:

private void VisitJObject(JObject jObject) { foreach (var property in jObject.Properties()) { EnterContext(property.Name); VisitProperty(property); ExitContext(); } } private void EnterContext(string context) { _context.Push(context); _currentPath = string.Join(":", _context.Reverse()); } private void ExitContext() { _context.Pop(); _currentPath = string.Join(":", _context.Reverse()); }

看来 ASP.NET 团队应该留下更多有启发性的签入评论:)。

我最好的猜测是,config.json 文件中可能存储有需要包含

.

 的数据,而 
:
 则不太常见。例如:

"AppSettings": { "Site.Title": "Is .NET getting faster?" },

这是一个不好的例子,但他们希望尽可能“安全”并使用超出规范的东西似乎是合理的。如果您想存储类型的全名,这也会稍微容易一些,而无需担心杂散期。

"AppSettings": { "ImportantTypeName": "WebApp.CoolStuff.Helpers.AwesomeClass" },
    

0
投票
不确定何时引入,我正在使用 .net 8。

假设您的 appSettings.json 如下所示:

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "SomeGroupedSettings": { "Setting1": "Setting 1 value", "Setting2": 42 } }
你有一个匹配的班级:

public class SomeGroupedSettings { public string? Setting1 { get; set; } public int Setting2 { get; set; } }
您可以通过注入配置(Microsoft.Extensions.Configuration.IConfiguration)来读取强类型对象,如下所示:

var tmp = configuration.GetSection(nameof(SomeGroupedSettings)).Get<SomeGroupedSettings>();
    
© www.soinside.com 2019 - 2024. All rights reserved.