Home > Software design >  C# Sending a JSON object with a list to WebAPI is coming up null
C# Sending a JSON object with a list to WebAPI is coming up null

Time:05-25

I have a class object that contains a list of user emails.

public class UserAccount
{
  public string UserName {get; set;}
  public List<string> UserEmails {get; set;}
}

When passing a json object in Insomnia to my web api post method. The username is populated but the UserEmails is null.

{
  "UserAccount":
  {
     "UserName": "StackOverflow",
     "UserEmails":[
    "[email protected]",
    "[email protected]"
    ]
  }
}

public async Task<ActionResult> DoUserStuff([FromBody] UserAccount account)
{
  // account.UserName is populated
 //  account.UserEmails are null
}

What am I missing with the JSON payload?

CodePudding user response:

your classes should be

     public class Data
    {
        public UserAccount UserAccount { get; set; }
    }

    public class UserAccount
    {
        public string UserName { get; set; }
        public List<string> UserEmails { get; set; }
    }

and action

public async Task<ActionResult> DoUserStuff([FromBody] Data data)
  • Related