public override void OnActionExecuted(ActionExecutedContext context) { if (context.Exception != null) { return; } var ignoreWrapper = IsIgnoreWrappedApi(context); if (!ignoreWrapper) { var wrapperData = GetWrapperData(context); var result = new NalongApiResponse(0, string.Empty, wrapperData); context.Result = new OkObjectResult(result); } }
private async Task HandlerException(HttpContext httpContext, Exception exception) { var error = Convert(exception); var errorData = new NalongApiResponse(exception.GetErrorNumber(), error.ErrorMessage, error); //异常-驼峰命名 var errorResponse = JsonConvert.SerializeObject(errorData, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } ); httpContext.Items["nl-items-errorCode"] = exception.GetErrorCode(); httpContext.Items["nl-items-middleware"] = "ExceptionMiddleware"; _logger.LogException(exception, errorResponse); //清除掉errorCode 污染审计中间件 httpContext.Items["nl-items-errorCode"] = ""; // httpContext.Response.StatusCode = (int)exception.GetHttpStatusCode(); // 这里强写200 httpContext.Response.StatusCode = 200; httpContext.Response.ContentType = "application/json"; await httpContext.Response.WriteAsync(errorResponse); }