【问题标题】:Can Web API HttpPost return ICollectionWeb API HttpPost 能否返回 ICollection
【发布时间】:2017-01-19 09:29:13
【问题描述】:

新手编写 Web 服务。我正在使用 C#/ASP.Net 和 WebAPI。最终目标是接收 JSON 集合,并将数据反序列化到数据库,并通知客户端应用程序任何失败的记录,哪个客户端将记录。

HTTPPost 能否通过 IHttpActionResult 或 HttpResponseMessage 返回失败行的集合(作为序列化的 Json),有点像这样:

[HttpPost]
public HttpResponseMessage Post([FromBody]List<Things> t)
{
    // deserialize t and process to database

    // list of failed records
    ICollection<Thing> things= new List<Thing>();
    things.Add(...);
    things.Add(...);

    string jsonFailedRows = 
      JsonConvert.SerializeObject(things, Formatting.Indented);

    // Write the list to the response body
    HttpResponseMessage response =     
       Request.CreateResponse(HttpStatusCode.OK, jsonFailedRows);
    return response;
}

我看到了这个链接:StackOverFlow,上面说我可以执行以下操作,但这对于帖子是否正确?

"如果你调用 ApiController.Ok() 方法,后者为你完成:

return Ok(jsonFailedRows);  

最后,有没有办法使用 CreatedAtRoute 来做到这一点?

【问题讨论】:

    标签: json web-services asp.net-web-api http-post


    【解决方案1】:

    链接响应中发布的解决方案确实回答了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 2017-08-08
      • 2014-07-24
      • 2014-05-07
      • 1970-01-01
      相关资源
      最近更新 更多