将一组json对象发送到web api,如何格式化url?

use*_*812 2 c# asp.net json asp.net-web-api

我用以下api编写了asp.net web-api项目:

控制器方法:Uri:

GetAllItems:/ api/items(works)

GetItem(int id)/ api/items/id(工作)和

GetListOfItem(IEnumerable <Items>项目)/ api/items /项目列表(不起作用)

功能类似于此(不关心逻辑)

public IHttpActionResult GetByArray(IEnumerable<Book> bks)
    {
        var returnItems = items.Select(it => it).Where(it => it.Price < bks.ElementAt(0).Price || it.Price < bks.ElementAt(1).Price);
        if (returnItems == null)
            return NotFound();
        else
        {
            return Ok(returnItems);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我使用邮递员发送请求和以下请求工作正常

http://localhost:50336/api/items/
http://localhost:50336/api/items/100
Run Code Online (Sandbox Code Playgroud)

但不是

http://localhost:50336/api/items/[{"Owner":"MySelf","Name":"C","Price":151},{"Owner":"Another","Name":"C++","Price":151}]
Run Code Online (Sandbox Code Playgroud)

我应该如何格式化最后一个请求,其中我有一个json格式的项目列表,以使其工作?

Chr*_*rdt 6

你想用a HttpPostAttributeFromBodyAttribute:装饰你的方法:

[HttpPost]
public IHttpActionResult GetByArray([FromBody]IEnumerable<Book> bks)
{
}
Run Code Online (Sandbox Code Playgroud)

然后发送json作为帖子正文.


你的邮差应该是这样的:

邮差