全局过滤器中排除指定Controller和action方法

本文介绍了一种解决全局过滤器中排除指定Controller和action方法的方法。通过自定义比较方法来判断控制器或action是否需要排除,并在自定义过滤器中应用此逻辑。在注册到全局过滤器后,例如排除了Home控制器的index方法,实现了按需避免GZIP压缩。欢迎大家提供更好的实现方式。
摘要由CSDN通过智能技术生成

最近一直被这个问题所困扰,现在想了一个方案,特贡献出来给大伙,技术不好,还请指教,如有更好的方法,还请告诉我,谢谢。

 1   /// <summary>
 2         /// 检查是否排除过滤器
 3         /// </summary>
 4         /// <param name="filterContext">方法执行上下文</param>
 5         /// <returns></returns>
 6         public bool IsRemoveFilter(ActionExecutingContext filterContext)
 7         {
 8             bool check = false;
 9             string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
10             if (this.RemoveActionName == null)
11             {
12                 check = CustomCompareStrings(RemoveControllerName, controllerName);
13             }
14             else
15             {
16                 string actionName = filterContext.ActionDescriptor.ActionName;
17                 if (CustomCompareStrings(RemoveControllerName, controllerName))
18                 {
19                     check = CustomCompareStrings(RemoveActionName, actionName);
20                 }
21             }
22             return check;
23         }

这其中,我自定义了一个比较方法,判断当前的控制器或者action方法是否设置了的排除对象。

 1  /// <summary>
 2         /// 自定义比较方法
 3         /// </summary>
 4         /// <param name="useSetValue">用户设置的排除过滤器的值</param>
 5         /// <param name="nowPageValue">当前所在页面的ActionName或ControllerName的值</param>
 6         /// <returns></returns>
 7         publ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值