fjompen fjompen - 16 days ago 6
ASP.NET (C#) Question

Web API not reading body in PUT

I'm trying to send a List to my controller via Web API. I'm sending it as JSON via Postman. I'm 100% sure the JSON is correctly formatted. Still the

usersList
ends up
null
. I've tried without the
[FromBody]
attribute also.

The controller name is
UserController
, so the url is
api/user/
. Method used is
Put
.

public IHttpActionResult Put([FromBody]List<UserVm> usersList)
{
if (usersList.Count > 0)
{
_userService.UpdateUserRoles(usersList);
return Ok();
}
return BadRequest();
}

public class UserVm
{
public int Id { get; set; }
public string UserName { get; set; }
public string Name { get; set; }
public string Token { get; set; }
public string Icao { get; set; }
public string RefreshToken { get; set; }
public int ExpiresIn { get; set; }
public List<Role> Roles { get; set; }
}

[
{
"id": 0,
"username": "banji",
"name": "baji",
"token": "bajz",
"icao": "poffe",
"refreshtoken": "konna",
"expiresin": 0,
"roles": [{
"id": 0,
"department": "asd",
"isadmin": false
}]
},
{
"id": 0,
"username": "banji",
"name": "baji",
"token": "bajz",
"icao": "poffe",
"refreshtoken": "konna",
"expiresin": 0,
"roles": [{
"id": 0,
"department": "asd",
"isadmin": false
}]
}
]


Suggestions on what I'm doing wrong are much appreciated.

Answer

I found the solution to my problem. I had forgotten to have an parameterless constructor..