How to configure nested objects from appsettings.json in .NET Core?

10,946

Nested options are supported and successfully deserialized from appsettings.json. However, fields instead of properties break built-in POCO object deserialization. With the properties the problem should disappear:

public API FirstAPI { get; set; }

public string Path { get; set; }
Share:
10,946
Steve Eggering
Author by

Steve Eggering

Updated on June 05, 2022

Comments

  • Steve Eggering
    Steve Eggering almost 2 years

    VS2015 Pro, .NET Core, Web Application.

    I'm trying to figure out how to get objects out of appsettings, that have nested objects. I can do it with single level objects by creating a POCO with simple types that match the config names. But with more complex objects, it's not working. I'm getting top level objects back, but they are coming back null. Here's what I have so far:

    StartUp.ConfigureServices:

    enter image description here

    appsettings.json:

    enter image description here

    POCO APIContext:

    enter image description here

    Using Class:

    enter image description here

    So I get my three API objects back but they are all null. I don't know enough about the Configuration in Startup.cs to know what it's supposed to look like, but here it is.

    enter image description here

  • Steve Eggering
    Steve Eggering about 7 years
    Thank you, this works. And sorry about the screenshots, I will correct that on future posts.