Microsoft.Configuration.Extensions:如何获取节/复杂值作为json字符串? [英] Microsoft.Configuration.Extensions: How to get section / complex value as json string?

查看:40
本文介绍了Microsoft.Configuration.Extensions:如何获取节/复杂值作为json字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们有这样的配置

// 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆