为OData中的主键获取多个记录

时间:2019-10-09 07:38:59

标签: odata

对于以下查询,我应该只获得一条记录,其中主键ID = 1,但是我会获得所有记录的列表。

{"books": [
   {"title": "HTTP-Protocol", "image": "http.jpg", "description": "A standard book on HTTP", "status": "Available"},
   {"title": "In between", "image": "destroy.jpg", "description": "Not RESTFul?", "status": "Available"},
   {"title": "JSON-Patch", "image": "patching.jpg", "description": "Explanation on how to use json-patch in RESTFul", "status": "Planned"},
   {"title": "RESTFul", "image": "fielding.jpg", "description": "Representational state transfer", "status": "Available"}
]}

我正在使用Postman在OData版本4的ASP.net Web API上进行查询。

控制器代码:

http://localhost:52484/Schools(1) 

关于为什么要获取所有记录以及如何只获取其中一个记录的任何想法?

1 个答案:

答案 0 :(得分:0)

您必须为此在控制器中实现一个方法:

[HttpGet]
public IActionResult GetByKey([FromODataUri]int key)
{
    var res = _schoolService.Schools().FirstOrDefault(s => s.Id == key);
    return res != null ? Ok(res) : (IActionResult)NotFound();
}