【发布时间】:2016-01-06 10:02:16
【问题描述】:
我的ProductsController 中有这样的功能:
public IHttpActionResult GetProduct(string id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
return Ok(product);
}
当我使用此 URL 发送 GET 请求时:
api/products?id=
它将id 视为空值。我怎样才能让它把它当作一个空字符串?
【问题讨论】:
-
GetProduct(string id = string.Empty)
-
使用可选参数
string id = ""则可以调用GET api/products/ -
@Ric 如果我希望 GET api/products 返回错误怎么办?因为我觉得会模棱两可
-
在什么意义上模棱两可?这取决于您如何设置路由等,以及您是否使用 resful api,
标签: c# asp.net asp.net-web-api null query-string