public override void OnActionExecuted(ActionExecutedContext context) { if (context.Exception != null && context.ExceptionHandled == false) { context.ExceptionHandled = true; int errcode = 500; if (context.Exception is ErrorCodeException) { errcode = (int)(context.Exception as ErrorCodeException).ErrorCode; } else { //其他异常,需要记日志 try { ILogger logger = Global.ServiceProvider.GetService<ILogger<BaseController>>(); logger.LogError(context.Exception, ""); } catch { } } context.Result = new JsonResult(new { code = errcode, msg = context.Exception.Message }); } else if (context.Exception == null && context.Result != null && context.Result is BadRequestObjectResult) { var objResult = (BadRequestObjectResult)context.Result; if (objResult.Value is ValidationProblemDetails details) { context.Result = new JsonResult(new { code = 400, msg = details.Errors.ToJsonString() }); } else { context.Result = new JsonResult(new { code = 400, msg = "server error" }); } } else if (context.Exception == null && context.Result != null && context.Result is ObjectResult) { var objResult = (ObjectResult)context.Result; context.Result = new JsonResult(new { code = 200, data = objResult.Value }); } else if (context.Exception == null && context.Result is EmptyResult) { context.Result = new JsonResult(new { code = 200 }); } base.OnActionExecuted(context); } }