【发布时间】:2017-05-18 14:23:05
【问题描述】:
我有一个问题,在 webapi 中返回数据的最佳方式是什么。 例如,我们可以有 2 个场景。
1) GetProductsById,我们在其中接收一个 ID 并返回该 ID 的数据。
2) GetProducts 我们在其中返回数据列表
所以对于 GetProductsById 我们可以这样做:
public IHttpActionResult GetProduct(int id)
{
var product = getProducts().FirstOrDefault((p) => p.Id == id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
获取列表:
public IHttpActionResult GetProduct(int id)
{
var products = getproducts();
if (products == null)
{
throw new NotFoundException()
}
return Ok(product);
}
我想知道在这两种情况下处理未找到情况的最佳方法。
【问题讨论】:
-
最好的方法是应用程序的其余部分在遇到这种情况时已经做了什么。您的问题是基于意见和题外话
标签: c# asp.net web-services rest asp.net-web-api