c# – ASP.Net核心注入设置
作者:互联网
在ASP.Net Core中,可以使用IOptions< T>将配置值注入类中.
所以,如果我有以下appsettings.json配置:
{
"CustomSection": {
"Foo": "Bar"
},
"RootUrl": "http://localhost:12345/"
}
我可以注入IOptions< CustomSection>进入我的构造函数(假设我已经定义了一个CustomSection类)并读取了Foo属性.
如何将RootUrl设置注入构造函数或不支持?
解决方法:
来自文档
Using options and configuration objects是不可能的:
The options pattern enables using custom options classes to represent a group of related settings. A class needs to have a public read-write property for each setting and a constructor that does not take any parameters (e.g. a default constructor) in order to be used as an options class.
这意味着您需要生成一个类来读取它的配置值.但是在你的样本中,RootUrlcan不能通过类构建.
标签:c,dependency-injection,asp-net,asp-net-core-1-0 来源: https://codeday.me/bug/20190717/1487550.html