- IAuthorizationFilter 可用于登录检查,判断是否登录
- IActionFilter Action拦截器 方法执行前后拦截
- IResultFilter Result拦截器 方法返回前后拦截
- ExceptionFilter Exception拦截器 异常处理拦截 同一返回错误界面
- FilterAttribute ||1/2/3/4 自定义拦截器(多继承上面任意拦截器实现)
IAuthorizationFilter
public class LoginFilter : IAuthorizationFilter { public void OnAuthorization(AuthorizationContext filterContext) { //1、控制器+方法 string controllerName1 = filterContext.RouteData.Values["controller"].ToString(); string actionName1 = filterContext.RouteData.Values["action"].ToString(); //2、建议: string controllerName2 = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; string actionName2 = filterContext.ActionDescriptor.ActionName; //filterContext.HttpContext.Response.Write("控制器:" + controllerName2 + "<br/>"); //filterContext.HttpContext.Response.Write("Action:" + actionName2 + "<br/>"); if (controllerName2 == "Login" && actionName2 == "index"|| controllerName2 == "Exception") { } else { if (filterContext.HttpContext.Session["username"]==null) { filterContext.Result = new ContentResult() {Content="没有登录" }; } } } }
IActionFilter .
public class ActionFilter : IActionFilter { public void OnActionExecuted(ActionExecutedContext filterContext) { string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; string actionName = filterContext.ActionDescriptor.ActionName; File.AppendAllText(HttpContext.Current.Server.MapPath("~/IActionFilter.txt"), $"OnActionExecuted: {controllerName}-{actionName} "); } public void OnActionExecuting(ActionExecutingContext filterContext) { string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; string actionName = filterContext.ActionDescriptor.ActionName; File.AppendAllText(HttpContext.Current.Server.MapPath("~/IActionFilter.txt"), $"OnActionExecuting: {controllerName}-{actionName} "); } }
IResultFilter
public class ResultFilter : IResultFilter { public void OnResultExecuted(ResultExecutedContext filterContext) { File.AppendAllText(HttpContext.Current.Server.MapPath("~/IResultFilter.txt"), $"OnResultExecuted "); } public void OnResultExecuting(ResultExecutingContext filterContext) { File.AppendAllText(HttpContext.Current.Server.MapPath("~/IResultFilter.txt"), $"OnResultExecuting "); } }
ExceptionFilter :
public class ExceptionFilter : IExceptionFilter { public void OnException(ExceptionContext filterContext) { //1、获取异常对象 Exception ex = filterContext.Exception; //2、记录异常日志 File.AppendAllText(HttpContext.Current.Server.MapPath("~/IExceptionFilter.txt"), $"OnException {ex.Message} "); //3、重定向友好页面 filterContext.Result = new RedirectResult("~/Exception/NoFund"); //4.标记错误已处理 filterContext.ExceptionHandled = true; } }
FilterAttribute
/// <summary> /// 自定义拦截 /// </summary> public class LogFilterAttribute : FilterAttribute, IActionFilter { public void OnActionExecuted(ActionExecutedContext filterContext) { string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; string actionName = filterContext.ActionDescriptor.ActionName; File.AppendAllText(HttpContext.Current.Server.MapPath("~/LogFilterAttribute.txt"), $"OnActionExecuted: {controllerName}-{actionName} "); } public void OnActionExecuting(ActionExecutingContext filterContext) { string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; string actionName = filterContext.ActionDescriptor.ActionName; File.AppendAllText(HttpContext.Current.Server.MapPath("~/LogFilterAttribute.txt"), $"OnActionExecuting: {controllerName}-{actionName} "); } }
结果显示: