有人可以帮助我创建一个异常,该异常具有被覆盖的消息、源和堆栈跟踪值。
我尝试在 HttpResponseMessage 中使用 httperror,但我还需要将 Stacktrace 和 Source 添加到响应异常中。这是我的代码:
public Product GetProductById(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
HttpResponseMessage errorResponse = null;
if (product == null)
{
//using httperror
var message = String.Format("Customer with id: {0} was not found", id);
var httpError = new HttpError(message);
errorResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, httpError);
throw new HttpResponseException(errorResponse);
}
return product;
}
输出:{ Message: "Customer with id: 7 was not found" }