简体   繁体   English

如何在使用httpclient发布的Web API控制器中接收Json可序列化对象数组

[英]how to receive Json serializable object array in web api controller posted using httpclient

I am sending data using HttpClient like below 我正在使用HttpClient发送数据,如下所示

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    streamWriter.Write(JsonConvert.SerializeObject(message).ToArray());
}

what would be the web api controller method parameter i am going to use like below 我将使用如下所示的Web api控制器方法参数

public IHttpActionResult ReceivedMessages([FromBody] IList<char> messages)

Instead of IList i have tried char[] array and string[] array also. 除了IList我还尝试了char[]数组和string[]数组。 i am getting null value always. 我总是得到空值。

But if i checked this request.Content.ReadAsStringAsync().Result i am receiving the value in string format but in controller class i am getting null 但是如果我检查了这个request.Content.ReadAsStringAsync().Result我正在接收字符串格式的值,但是在控制器类中我会得到null

如果您在请求发送者处有消息对象类型的副本,也许是MessageClass ,则在Web api端使用与Request类MessageClass相同的类型,并使用

public IHttpActionResult ReceivedMessages([FromBody] List<MessageClass> messages);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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