Asp.Net Core MVC 过滤器

过滤器是什么

简单理解就和净水器一个原理。请求进来之后过滤请求用的。

过滤器的应用

这里的发现一个很有意思的地方,.net Core2.1的过滤器和.net Core 3.1 的过滤器使用方法是有点区别的,.net Core 3.1的使用方法更优雅了。

在.net Core 中过滤器的允许顺序是:授权过滤器->资源过滤器->操作过滤器->异常过滤器->结果过滤器

这里是.Net Core 2.1里的写法

在.net Core 3.1里filterContext.HttpContext.Response.Writer()这个方法没有了

 public class MyActionFilterAttribute:ActionFilterAttribute
    {
         //请求过滤
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //获取此次请求的控制器
            string strController = filterContext.RouteData.Values["controller"].ToString();
            //获取此次请求的方法名称
            string strAction = filterContext.RouteData.Values["action"].ToString();
            //请求id
            string Id = filterContext.ActionDescriptor.Id.ToString();
            filterContext.HttpContext.Response.Writer("控制器名称" + strController);
            filterContext.HttpContext.Response.Writer("方法名称" +strAction);
            filterContext.HttpContext.Response.Writer("请求id" + Id);
            filterContext.HttpContext.Response.Writer("请求时间开始" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
          base.OnActionExecuting(filterContext);
        }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.Writer("请求返回时间"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            base.OnActionExecuted(filterContext);
        }

这是.net Core 3.1里的写法

1.方法一:还是可以根据2.1中的使用方法去用。但是不够优雅,这里是重写的父类的纯虚函数

 public class MyActionFilterAttribute:ActionFilterAttribute
    {
         //请求过滤
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //获取此次请求的控制器
            string strController = filterContext.RouteData.Values["controller"].ToString();
            //获取此次请求的方法名称
            string strAction = filterContext.RouteData.Values["action"].ToString();
            //请求id
            string Id = filterContext.ActionDescriptor.Id.ToString();
            filterContext.HttpContext.Response.Headers.Add("控制器名称" , strController);
            filterContext.HttpContext.Response.Headers.Add("方法名称" , strAction);
            filterContext.HttpContext.Response.Headers.Add("请求id" , Id);
            filterContext.HttpContext.Response.Headers.Add("请求时间开始" , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
          base.OnActionExecuting(filterContext);
        }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.Headers.Add("请求返回时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            base.OnActionExecuted(filterContext);
        }
    }

2.方法二:实现微软给我们提供的接口 IActionFilter,效果其实差不多,只是少去了调用父类方法那一步

 public class MyActionFilterAttribute: IActionFilter
    {
         //请求过滤
        public  void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //获取此次请求的控制器
            string strController = filterContext.RouteData.Values["controller"].ToString();
            //获取此次请求的方法名称
            string strAction = filterContext.RouteData.Values["action"].ToString();
            //请求id
            string Id = filterContext.ActionDescriptor.Id.ToString();
            filterContext.HttpContext.Response.Headers.Add("控制器名称" , strController);
            filterContext.HttpContext.Response.Headers.Add("方法名称" , strAction);
            filterContext.HttpContext.Response.Headers.Add("请求id" , Id);
            filterContext.HttpContext.Response.Headers.Add("请求时间开始" , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
          
        }
        public  void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.Headers.Add("请求返回时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

或与且与或非

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值