Pupkin
Pupkin

Reputation: 1111

How to bind appsettings.json to class?

I have an appsettings.json file in my C# app. It contains field with names in upper case:

{
    "FIELD_ONE": "foo",
    "FIELD_TWO": "bar"
}

But I would like to bind it to class with camel-case naming style:

public class MySettings
{
    public string FieldOne { get; set; }
    public string FieldTwo { get; set; }
}

How can I bind this appsettings to MySettings instance?

Upvotes: 0

Views: 149

Answers (1)

bmo
bmo

Reputation: 86

This could be a solution:

public class MySettings
{
     [JsonProperty("FIELD_ONE")]
     public string FieldOne { get; set; }
     [JsonProperty("FIELD_TWO")]
     public string FieldTwo { get; set; }
}

Upvotes: 1

Related Questions