想象一下,我在 appsettings.json 中有以下設定:
{
"OtherSettings" : {
"id" : 3
},
"Needed" : {
"Database" : {
"ConnectionString" : "abc123"
},
"Interval" : {
"is_active" = true,
"in_seconds" = "15"
}
}
}
對于 Database 部分,我有一個DatabaseSettings帶有 property的類ConnectionString。
對于 Interval 部分,我有 class IntervalSettings。
如果我想將所有這些設定系結Needed到一個類中NeededSettings,我需要什么屬性以及如何使用依賴注入系結資料庫類的選項?目前我正在這樣做:
services.Configure<DatabaseSettings>Configuration.GetSection("Needed:Database"));
services.Configure<IntervalSettings>(Configuration.GetSection("Needed:Interval"));
所以我用 2 行做這個,但我想用 1 行做,并將所有設定系結到新類 NeededSettings
uj5u.com熱心網友回復:
只需洗掉這兩行并添加:
services.Configure<NeededSettings>(Configuration.GetSection("Needed"));
并NeededSettings在您使用過的所有地方使用DatabaseSettings和IntervalSetttings。
編輯:
public class NeededSettings{
public DatabaseSettings Database {get;set;}
public IntervalSettings Interval {get;set;}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/355495.html
上一篇:頁面加載時禁用“添加新記錄”按鈕
