实施
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"));