为什么Http代码500返回,虽然我捕获了异常
本文关键字:异常 代码 Http 返回 为什么 | 更新日期: 2023-09-27 18:01:27
虽然我的if-子句在我的fiddler中执行,但我看到http错误500而不是404,为什么会这样?
public class GlobalExceptionHandler : ExceptionHandler
{
public override void Handle(ExceptionHandlerContext context)
{
if (context.Exception is ResourceNotFoundException)
{
context.Request.CreateErrorResponse(HttpStatusCode.NotFound, context.Exception.Message);
}
else
{
base.Handle(context);
}
}
}
这是我在fiddler Response:
中得到的结果HTTP/1.1 500 Internal Server Error
Content-Length: 3945
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Wed, 08 Jul 2015 08:14:45 GMT
{"Message":"An error has occurred.","ExceptionMessage":"The Resource could not be found!","ExceptionType":"ErpApplicationEndpoints.ResourceNotFoundException","StackTrace":" at ... Removed for clarity...
您需要在if子句中设置ExceptionHandlerContext
对象上的Result
属性,而不是做CreateErrorResponse
(这也是不正确的,因为您没有将创建的HttpResponseMessage
分配给任何东西)
context.Result = <a-httpactionresult-instance-here>