Microsoft.Extensions.Configuration有自己的应用程序接口,用于浏览它读入的配置文件中包含的JSON。(这是ASP.NET用于配置的内容)
对于给定的JSON节点-有没有办法以字符串而不是更多的配置对象的形式访问其内容?我的配置文件中有JSON对象,我需要通过JSON反序列化程序运行这些对象(所以我只想从文件中以字符串的形式读取这个节点)。
类似于下面的内容:
var myObjectsSection = configuration.GetSection("MyObjects");
var innerText = myObjectsSection.InnerText; //Is there any way to do this???
var myObjs = JsonConvert.DeserializeObject<MyObject[]>(innerText);配置文件:
{
"SomeSetting": "mySetting",
"MyObjects": [
{
...
},
{
...
}
]
}发布于 2020-07-14 01:05:41
Asp.net核心3有一个获取与类型相关的配置值的方法:T IConfigurationSection.Get<T>()
我已经尝试解析您所描述的自定义配置,它正在工作。
appsetting.json:
{
"CustomSection": [
{
"SomeProperty": 1,
"SomeOtherProperty": "value1"
}
]
}启动类:
public class Startup
{
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
IConfigurationSection section = this.Configuration.GetSection("CustomSection");
var configs = section.Get<List<CustomSectionClass>>();
}
public class CustomSectionClass
{
public int SomeProperty { get; set; }
public string SomeOtherProperty { get; set; }
}
}https://stackoverflow.com/questions/62879170
复制相似问题