1

VS2015 Pro,.NET核心,Web應用程序。如何在.NET Core中從appsettings.json配置嵌套對象?

我想弄清楚如何從appsettings,有嵌套的對象中獲取對象。我可以通過使用與配置名稱匹配的簡單類型創建POCO來實現單層對象。但是對於更復雜的對象,它不起作用。我收到頂級對象,但它們將返回null。這是我到目前爲止有:

StartUp.ConfigureServices:

enter image description here

appsettings.json:

enter image description here

POCO APICONTEXT:

enter image description here

使用類:

enter image description here

所以我讓我的三個API對象回來,但它們都是空。 我對Startup.cs中的配置知之甚少,不知道它應該是什麼樣子,但它在這裏。

enter image description here

+0

您遇到什麼錯誤? – koelkastfilosoof

+0

@koelkastfilosoof對不起,我在那個問題的打字中做了一些修改。沒有更多的錯誤,但我更新了問題,以顯示我的API對象返回null。 –

+0

你可以在你的'Startup'的'Configure'方法中放置一個斷點並檢查'IConfiguration'?驗證所有設置是否正確加載 – koelkastfilosoof

回答

1

嵌套選項支持和成功appsettings.json反序列化。但是,字段而不是屬性會破壞內置的POCO對象反序列化。隨着屬性問題應該消失:

public API FirstAPI { get; set; } 

public string Path { get; set; } 
+0

謝謝,這個作品。對於屏幕截圖抱歉,我將在未來的帖子中糾正這一情況。 –