添加实体列表作为参数

时间:2018-02-09 10:33:11

标签: c# asp.net-web-api odata asp.net-web-api-odata

是否可以在c#odata中检索实体列表作为操作的参数?

尝试以下操作,但它会使整个控制器无效:

[HttpPost]
[NhSessionManagement()]
[ODataRoute("BatchUpdate")]
public async Task<IHttpActionResult> BatchUpdate(List<Item> items, bool updateDefaultJSONFile)
{
    return Ok();
}

这是配置:

        {
            var action = builder.EntityType<Item>().Collection.Action("BatchUpdate");

            action.CollectionParameter<Item>("items");
            action.Parameter<bool>("updateDefaultJSONFile");
        }

这是我发送数据的方式:

$http.post(appConfig.serviceUrl + "api/items/Service.BatchUpdate", { items: itemsToUpdate, updateDefaultJSONFile: true}).success(function (data, status, headers, config) {
    debug("Successfully saved");
}).error(function (data, status, headers, config) {
    debug("Failed to save");
});

在小提琴手中:

POST http://192.168.20.108/api/items/Service.BatchUpdate HTTP/1.1
Host: 192.168.20.108
Connection: keep-alive
Content-Length: 83114
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
Origin: http://192.168.20.108
Authorization: Bearer ****
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://192.168.20.108/
Accept-Encoding: gzip, deflate
Accept-Language: sv-SE,sv;q=0.9,en-US;q=0.8,en;q=0.7

{"items":[{"Key":"helpful","Text":"test"}],"updateDefaultJSONFile":false}

1 个答案:

答案 0 :(得分:1)

将集合参数指定为数组,而不是列表:

[HttpPost]
[NhSessionManagement()]
[ODataRoute("BatchUpdate")]
public async Task<IHttpActionResult> BatchUpdate(Item[] items, bool updateDefaultJSONFile)
{
    return Ok();
}

配置是正确的,干得好。