using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Threading.Tasks; using Ys.Core.Common; namespace djyweb_djyPaasApi { public class YsExceptionfilter : ActionFilterAttribute,IExceptionFilter { public void OnException(ExceptionContext context) { var rs = new ReturnResult(); var response = context.HttpContext.Response; response.StatusCode = 200; response.WriteAsync(JsonSerializer.Serialize(rs, new JsonSerializerOptions(JsonSerializerDefaults.Web))).ConfigureAwait(false); } //在Action执行之前由 MVC 框架调用。 public new void OnActionExecuting(ActionExecutingContext filterContext) { var rs = new ReturnResult(); var response = filterContext.HttpContext.Response; response.StatusCode = 200; filterContext.Result = new BadRequestObjectResult(rs); response.WriteAsync(JsonSerializer.Serialize(rs, new JsonSerializerOptions(JsonSerializerDefaults.Web))).ConfigureAwait(false); } //在Action执行之后由 MVC 框架调用。 public new void OnActionExecuted(ActionExecutedContext filterContext) { var rs = new ReturnResult(); var response = filterContext.HttpContext.Response; response.StatusCode = 200; filterContext.Result = new BadRequestObjectResult(rs); response.WriteAsync(JsonSerializer.Serialize(rs, new JsonSerializerOptions(JsonSerializerDefaults.Web))).ConfigureAwait(false); } //在执行Result之前由 MVC 框架调用。 public new void OnResultExecuting(ResultExecutingContext filterContext) { var rs = new ReturnResult(); var response = filterContext.HttpContext.Response; response.StatusCode = 200; filterContext.Result = new BadRequestObjectResult(rs); response.WriteAsync(JsonSerializer.Serialize(rs, new JsonSerializerOptions(JsonSerializerDefaults.Web))).ConfigureAwait(false); } //在执行Result后由 MVC 框架调用。 public new void OnResultExecuted(ResultExecutedContext filterContext) { var rs = new ReturnResult(); var response = filterContext.HttpContext.Response; response.StatusCode = 200; response.WriteAsync(JsonSerializer.Serialize(rs, new JsonSerializerOptions(JsonSerializerDefaults.Web))).ConfigureAwait(false); } } }