我正在处理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;
}
但是,如果id为null,在VS2010中会产生另一个错误,如:
"处理HTTP请求时发生异常。请查看此异常的'Response'属性返回的HTTP响应以获取详细信息"。
我该如何解决这个问题?
提前感谢您的帮助...