用过滤实现一个 当程序发生错误的时候,执行另一个方法的功能
一、控制器代码
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Common; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace ZanLveCore.Controllers { [Route("api/[controller]")] [ApiController] public class TestController : ControllerBase { /// <summary> /// 方法1 /// </summary> /// <param name="data">传参数据</param> /// <returns></returns> [CoreExceptionFilter(nameof(TestController), nameof(GetData2))] [HttpGet("{data}")] //必须{data} [HttpGet]则会报错 public string GetData(string data) { throw new Exception(); } /// <summary> /// 方法2 /// </summary> /// <param name="data">传参数据</param> /// <returns></returns> [HttpGet] public ApiResultModels GetData2(string data) { var result = new ApiResultModels(); result.Data = ""; result.Message = "Token获取成功!"; result.Code = "OK!"; return result; } } public class UserModel { public int Delete(int id) { //记录日志 //重新执行一遍代码 return id; } } }
二、过滤器方法
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; namespace ZanLveCore { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public class CoreExceptionFilter : Attribute, IExceptionFilter, IActionFilter { /// <summary> /// 发生错误的时候重新执行的方法 /// </summary> public string FallBackClass { get; set; } /// <summary> /// 发生错误的时候重新执行的方法 /// </summary> public string FallBackMethod { get; set; } /// <summary> /// 获取方法的参数 /// </summary> public object[] InvokeParameters { get; set; } /// <summary> /// 构造函数使用该类时参数为方法 /// </summary> /// <param name="fallBackMethod"></param> public CoreExceptionFilter(string fallBackClass, string fallBackMethod) { this.FallBackMethod = fallBackMethod; this.FallBackClass = fallBackClass; } /// <summary> /// 使用新方法 /// </summary> /// <param name="asm"></param> /// <param name="parameters"></param> /// <returns></returns> private object UseNewMethod(Assembly asm, object[] parameters) { Object obj = null; foreach (Type type in asm.GetExportedTypes()) { if (type.Name == FallBackClass) { obj = System.Activator.CreateInstance(type); foreach (var item in type.GetMethods()) { if (item.Name == FallBackMethod) { obj = type.GetMethod(FallBackMethod).Invoke(obj, parameters); } } } } return obj; } /// <summary> /// 获取所有被监控方法的参数 /// </summary> /// <param name="context"></param> public void OnActionExecuting(ActionExecutingContext context) { object[] parameters = new object[context.ActionArguments.Count]; int Count = 0; foreach (var item in context.ActionArguments) { parameters[Count] = item.Value; Count++; } InvokeParameters = parameters; } /// <summary> /// 错误的时候执行新的方法 /// </summary> /// <param name="context"></param> public void OnException(ExceptionContext context) { var objectResult = context.Exception as Exception; if (objectResult.Message != null) { context.Result = new ObjectResult(UseNewMethod(this.GetType().Assembly, InvokeParameters)); //context.Result = new ObjectResult(new { Success = true, code = 200, msg = "成功", Data = UseNewMethod(this.GetType().Assembly, InvokeParameters) }); } } public void OnActionExecuted(ActionExecutedContext context) { } } }