• MVC过滤器基本使用


        Action过滤器
    /// <summary>
    /// 执行代码前执行
    /// </summary>
    /// <param name="filterContext"></param>
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
      //1.显示触发过滤器action方法的名称
      string actionName = filterContext.ActionDescriptor.ActionName;
       filterContext.HttpContext.Response.Write("ActionName=" + actionName + "</br>");
      //2.显示触发过滤器action方法所在的控制器
      string ctrlName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
      filterContext.HttpContext.Response.Write("ControllerName=" + ctrlName + "</br>");
      //3.判断当前action方法是否贴有[HttpGet]特性标签
      bool isTrue = filterContext.ActionDescriptor.IsDefined(typeof(HttpGetAttribute), false);
      filterContext.HttpContext.Response.Write("当前action是否贴有[httpGet]特性" + isTrue + "</br>");
      //4.获取当前action方法上贴有[HttpGet]特性标签的实例
      object[] httpGets = filterContext.ActionDescriptor.GetCustomAttributes(typeof(HttpGetAttribute), false);
      foreach (object obj in httpGets)
      {
      filterContext.HttpContext.Response.Write("获取当前action方法上贴有[HttpGet]特性标签的实例:" + obj.ToString() + "</br>");
      }
      //5.记录当前action方法的实际参数
      ParameterDescriptor[] dics = filterContext.ActionDescriptor.GetParameters();
      foreach(ParameterDescriptor dic in dics)
      {
      //打印action参数名称
      filterContext.HttpContext.Response.Write("记录当前action方法的实际参数:" + dic.ParameterName.ToString() + "</br>");
      }
      base.OnActionExecuting(filterContext);
    }
    /// <summary>
    /// 执行代码后执行
    /// </summary>
    /// <param name="filterContext"></param>
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
      base.OnActionExecuted(filterContext);
    }
    /// <summary>
    /// 返回结果前执行
    /// </summary>
    /// <param name="filterContext"></param>
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        base.OnResultExecuting(filterContext);
    }
    /// <summary>
    /// 返回结果后执行
    /// </summary>
    /// <param name="filterContext"></param>
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        base.OnResultExecuted(filterContext);
    }
     
    异常过滤器:HandleErrorAttribute
    public override void OnException(ExceptionContext filterContext)
    {
        //1.获取异常对象
        Exception ex= filterContext.Exception;
        //2.将异常写入日志
        //2.1写入记录日志的具体实现过程略...
        //3.告诉mvc框架,过滤器已处理异常,不需要额外处理
        filterContext.ExceptionHandled = true;
        base.OnException(filterContext);
    }
     
    配置错误页:
    1 <customErrors mode="On" defaultRedirect="Error">
    2     <error statusCode="404" redirect="~/ErrorMgr/P404" />
    3     <error statusCode="500" redirect="~/ErrorMgr/P500"/>
    4 </customErrors>
  • 相关阅读:
    阿里P7架构师是如何解决跨域问题的!你有遇到吗?
    Node.js Express 框架
    用户管理 之 Linux 系统中的超级权限的控制
    [转] Exchange 2013 安装部署详解
    Clustered和Nonclustered Indexes 各自得特点和区别及长短处
    配置IIS5.5/6.0 支持 Silverlight
    redis模块使用
    redis介绍及安装
    linux上安装redis、远程配置及开机启动
    远程连接linux、配置SSHD
  • 原文地址:https://www.cnblogs.com/notniu/p/3898265.html
Copyright © 2020-2023  润新知