Microsoft.Configuration.Extensions:如何获取节/复杂值作为json字符串? [英] Microsoft.Configuration.Extensions: How to get section / complex value as json string?
问题描述
当我们有这样的配置
// appsettings.json
{
"SomeServiceConfiguration": {
"Server": "127.0.0.1",
"Port": "25"
}
}
可以使用绑定来访问数据:
it is possible to use binding to access data:
IConfiguration configuration = ...;
var section = configuration.GetSection("SomeServiceConfiguration");
var val = section.Value; // this is null
var t = new SomeServiceConfiguration();
section.Bind(t);
但是有可能获得值(节内容)就像字符串一样" (实际上是json) {"Server":"127.0.0.1","Port":"25"}
吗?
But is it possible to get value (section content) "just as string" (by the fact as json) {"Server": "127.0.0.1", "Port": "25"}
?
推荐答案
According to ConfigurationSection Class
this is not directly possible.
However, you could serialize to XML using the ConfigurationElement.SerializeElement(XmlWriter, Boolean)
Method, which is possible by default. You would have to convert to JSON afterwards, so this seems overkill.
我建议构建一个新的 JSON对象
并直接访问这些部分的值.
I would recommend building a new JSON Object
and accessing the section values directly.
这篇关于Microsoft.Configuration.Extensions:如何获取节/复杂值作为json字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!