发布对象的数组MVC的Web API [英] Posting array of objects with MVC Web API

查看:157
本文介绍了发布对象的数组MVC的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的操作后的 RecordIem 的单个对象的作品。
我希望做的是使用相同的格式发布请求的数组做同样的动作,但在大头。

I have a basic post operation that works on a single object of RecordIem. What I would like to do is do the same action but in bulk by posting an array of requests using the same format.

例如:

public HttpResponseMessage Post(RecordItem request)
{
    var recordItems = _recorder.RecordItem(request);
    return Request.CreateResponse(HttpStatusCode.OK, recordItems);
}

而当我张贴JSON:

And when I post the Json:

{
    Id : "7UP24fVkGOxSjrcclghe_mP2-po",
    System : 1,
    Environment : "Production"
}

一切工作正常。我想发布的Json类似于:

everything works fine. I would like to post Json similar to:

{
    Id : "7UP24fVkGOxSjrcclghe_mP2-po",
    System : 1,
    Environment : "Production"
},
{
    Id : "ClPE188H4TeD2LbQPeV_EzCsKVM",
    System : 1,
    Environment : "Production",
    Label : "RestTest1"
},
{
    Id : "SAWTMJzm-_AFqoNw70-gLeUzB4k",
    System : 1,
    Environment : "Production"
}

和有类似的方法下面,选中这些:

And have a method similar to below pick this up:

public HttpResponseMessage Post(RecordItem[] request)
{
    var recordItems = _recorder.RecordItems(request);
    return Request.CreateResponse(HttpStatusCode.OK, recordItems);
}

我已经尝试过使用两个 [FromBody] [ModelBinding] 属性阵列上使用尝试不同类型(列表,IList的,IEnumerable的),但无济于事。当使用 [FromBody] 请求参数是键,当使用 [ModelBinding] 列表是空的。我同时使用尝试和不工作的。

I've tried using both the [FromBody] and [ModelBinding] attributes on the array and tried using different types (List, IList, IEnumerable) but to no avail. When using [FromBody] the request parameter is null and when using [ModelBinding] the list is empty. I've tried using both and that doesn't work either.

我宁愿没有不必诉诸于我的客户循环单的帖子。

I'd rather not have to having to resort to looping single posts in my client.

感谢

推荐答案

由于您的发表预计的 RecordItem [] ,在你的请求主体的JSON内容应该是一个数组为好。

Since your Post expects an RecordItem[], your JSON content in your request body should be in an array as well.

你有什么是pretty接近 - 尝试添加​​一对括号的 [] 在你的数据:

What you have is pretty close -- try adding a pair of square bracket [] around your data:

[{
    Id : "7UP24fVkGOxSjrcclghe_mP2-po",
    System : 1,
    Environment : "Production"
},
{
    Id : "ClPE188H4TeD2LbQPeV_EzCsKVM",
    System : 1,
    Environment : "Production",
    Label : "RestTest1"
},
{
    Id : "SAWTMJzm-_AFqoNw70-gLeUzB4k",
    System : 1,
    Environment : "Production"
}]

这篇关于发布对象的数组MVC的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆