c# - 如何在使用 httpclient 发布的 web api Controller 中接收 Json 可序列化对象数组

标签 c# .net json asp.net-web-api

我正在使用 HttpClient 发送数据,如下所示

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

Web API Controller 方法参数是什么 我将像下面这样使用

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

我还尝试了 char[] 数组和 string[] 数组,而不是 IList。我总是得到空值。

但是如果我检查了这个 request.Content.ReadAsStringAsync().Result 我收到的是字符串格式的值,但是在 Controller 类中我得到的是 null

最佳答案

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

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

关于c# - 如何在使用 httpclient 发布的 web api Controller 中接收 Json 可序列化对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43634511/

相关文章:

json - Scala 发送 GET 请求,无法解析 json 响应

java - MongoDB 使用分层 JSON 进行更新 - 如何高效?点符号?

javascript - 在 forEach 循环后显示 HTML 时出现问题

c# - 使用 Visual Studio 2013 远程调试在 Linux 上运行的 C# mono 应用程序的最佳方法

c# - WPF 数据绑定(bind)和 IValueConverter

c# - CSV 中双引号值的正则表达式

.net - 将控制台应用程序转换为 Azure webjobs

c# - 使用 autofac 在 global.asax 中解析或注入(inject)依赖项

c# - Lucene docID 可靠性

c# - AsParallel() 顺序执行