为什么发生错误时邮递员会发回200 OK?

时间:2020-08-11 20:08:33

标签: c# asp.net-core error-handling postman

我已经设置了一些customException以在出现错误时返回:

private static Task HandleExceptionAsync(HttpContext context, Exception ex)
{
   var error = HttpStatusCode.InternalServerError; // 500 if unexpected

   if (ex is NotImplementedException) error = HttpStatusCode.NotImplemented;
   else if (ex is UnauthorizedAccessException) error = HttpStatusCode.Unauthorized;
   else if (ex is ArgumentException) error = HttpStatusCode.BadRequest;
   else if (ex is ArgumentNullException) error = HttpStatusCode.InternalServerError;

   var er = new ErrorResponse { error = new Dictionary<string, string> { { "message", ex.Message }, { "StatusCode", error.ToString() } } };

   var result = JsonConvert.SerializeObject(er);

   //Log to database
   Log.Error(ex, result);

   context.Response.ContentType = "application/json";

   return context.Response.WriteAsync(result);
}

因此,当我从邮递员发出邮递呼叫时,它返回正确的错误消息,但邮递员上显示的状态代码为200“确定”?可能是什么原因?

如果需要,我可以提供有关控制器中内容的更多信息。

0 个答案:

没有答案