【发布时间】:2015-05-20 02:29:57
【问题描述】:
我关注了一些examples for 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 数据的交互来看,. 符号通常可以正常工作,例如How to access nested json data。
另外,我发现了类似的SO question,但这并没有解释为什么选择:。
【问题讨论】:
标签: c# asp.net asp.net-core config.json