In one of my WebAPI 2 applications, I'm having trouble deserializing a List<string>
property of a FromBody
object. (The list stays empty, while the other properties are deserialized correctly.)
Whatever I do, the property only seems to deserialize correctly if I change the property to a string[]
. Unfortunately for me, the property needs to be of type List<string>
.
According to another question I found, I should be able to deserialize to a List<T>
, as long as T
is not an Interface
.
Is there anyone who has an idea what I could be doing wrong?
Controller:
public class ProjectsController : ApiController
{
public IHttpActionResult Post([FromBody]Project project)
{
// Do stuff...
}
}
Project object class:
public class Project
{
public string ID { get; set; }
public string Title { get; set; }
public string Details { get; set; }
private List<string> _comments;
public List<string> Comments
{
get
{
return _comments ?? new List<string>();
}
set
{
if (value != _comments)
_comments = value;
}
}
public Project () { }
// Other methods
}
Request JSON:
{
"Title": "Test",
"Details": "Test",
"Comments":
[
"Comment1",
"Comment2"
]
}