简体   繁体   中英

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":[
    "abc@asd.com",
    "xyz@asd.com"
    ]
  }
}

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

What am I missing with the JSON payload?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM