Web API中的自定义例外

时间:2013-06-13 23:22:27

标签: asp.net .net asp.net-mvc-4 asp.net-web-api

有人可以帮我创建一个具有被覆盖的消息,源和堆栈跟踪值的异常。

我尝试在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" }

1 个答案:

答案 0 :(得分:0)

您是否尝试过设置HttpConfiguration.IncludeErrorDetailPolicy属性?