zoukankan      html  css  js  c++  java
  • ASP.NET MVC Filter基础

    1. IAuthorizationFilter                                  可用于登录检查,判断是否登录
    2. IActionFilter                                                  Action拦截器 方法执行前后拦截
    3. IResultFilter                                                  Result拦截器 方法返回前后拦截
    4. ExceptionFilter                                             Exception拦截器 异常处理拦截    同一返回错误界面  
    5. 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="没有登录" };
                    }
                }
    
                                                                                                    
            }
        }
    View Code

    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} 
    ");
            }
        }
    View Code

    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 
    ");
            }
        }
    View Code

    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;                         
            }
        }
    View Code

    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} 
    ");
            }
        }

    结果显示:

  • 相关阅读:
    ES6获取页面所有页面标签名
    改变this对象的指向
    构造函数及原型
    全局对象this转为局部对象
    节点注意的问题
    Spark研究笔记17:设置 CVT
    Spark研究笔记15:资源 CVT
    Spark研究笔记13:Swing 组件类 CVT
    Spark研究笔记16:搜索 CVT
    Spark研究笔记19:插件体系开发 CVT
  • 原文地址:https://www.cnblogs.com/Zingu/p/14680465.html
Copyright © 2011-2022 走看看