【发布时间】:2018-07-17 13:56:46
【问题描述】:
实现IExceptionHandler,结果期待IHttpActionResult,但createResponse返回HttpResponseMessage。我想简单地从请求上下文中创建一个响应消息。它进行内容协商,我更喜欢使用已经存在的东西,而不是自己创建一个实现 IHttpActionResult 的自定义模型。
控制器可以访问帮助器以轻松地将 HttpResponseMessage 转换为 HttpActionResult,但是在控制器之外我找不到任何东西。我最好的选择是什么?
var myCustomException = context.Exception as MyCustomException;
if (myCustomException != null)
{
context.Result = context.Request.CreateResponse(myCustomException.StatusCode,
myCustomException.Error);
return;
}
context.Result = context.Request.CreateResponse(HttpStatusCode.InternalServerError,
new MyCustomError("Something went wrong"));
【问题讨论】:
标签: c# asp.net-web-api error-handling