I'm using this code to post some object to my Rest API:
public static string Post<T>(string uri, T data, string contentType = "application/json", string method = "POST")
{
byte[] dataBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
string str = JsonConvert.SerializeObject(data);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.ContentLength = dataBytes.Length;
request.ContentType = contentType;
request.Method = method;
using (Stream requestBody = request.GetRequestStream())
{
requestBody.Write(dataBytes, 0, dataBytes.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
and the code works just fine when posting a simple object:
[Route("api/Name/{ActivationKey}")]
public async Task<HttpResponseMessage> Post([FromBody] SomeKindOfObject value, string ActivationKey)
{
Some code...
}
But when I try posting a list of the same object I get null
in the controller:
[Route("api/Name/{ActivationKey}")]
public async Task<HttpResponseMessage> Post([FromBody] List<SomeKindOfObject> value, string ActivationKey)
{
Value is always null.
}
What could cause this and how can I fix it?
Try
public async Task<HttpResponseMessage> Post([FromBody] SomeKindOfObject[] value, string ActivationKey)
Desererialization can be contrary about its target container types, ie. list vs array vs collection.
Apparently the problem was that the list I tried to post was too big (more than 15k rows), what I did is splitting the list to shorter lists and posted every part alone and it worked.
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.