【发布时间】:2012-08-14 07:05:53
【问题描述】:
我在处理 MVC 4 webAPI 中的错误时遇到了一个大问题。在这里,我想验证传入的请求并返回一个 BadRequest 响应。
为此,我正在使用代码
public Product GetProduct(int id)
{
Product item = repository.Get(id);
if (item == null)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
return item;
}
但是如果是null id,VS2010会产生另一个类似的错误
“处理 HTTP 请求导致异常。有关详细信息,请参阅此异常的 'Response' 属性返回的 HTTP 响应”。
我该如何解决这个问题
提前谢谢....
【问题讨论】:
-
您使用哪个 URL 调用操作 GetProduct?
标签: asp.net-mvc-4 asp.net-web-api