Search code examples
c#jsonasp.net-web-apipostmaninsomnia

C# Sending a JSON object with a list to WebAPI is coming up null


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?


Solution

  • 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)