【发布时间】: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