最佳答案
我熟悉在.NET Core startup.cs 中将 appsetings.json 部分加载到强类型对象中。例如:
public class CustomSection
{
public int A {get;set;}
public int B {get;set;}
}
//In Startup.cs
services.Configure<CustomSection>(Configuration.GetSection("CustomSection"));
//Inject an IOptions instance
public HomeController(IOptions<CustomSection> options)
{
var settings = options.Value;
}
我有一个 appsetings.json 部分,其键/值对的数量和名称随着时间的推移而变化。因此,在类中硬编码属性名是不切实际的,因为新的键/值对需要在类中进行代码更改。一些键/值对的小示例:
"MobileConfigInfo": {
"appointment-confirmed": "We've booked your appointment. See you soon!",
"appointments-book": "New Appointment",
"appointments-null": "We could not locate any upcoming appointments for you.",
"availability-null": "Sorry, there are no available times on this date. Please try another."
}
有没有办法将这些数据加载到 MobileConfigInfo Dictionary 对象中,然后使用 IOptions 模式将 MobileConfigInfo 注入到控制器中?