全局過濾器中排除指定Controller和action方法

最近一直被這個問題所困擾,現在想了一個方案,特貢獻出來給大夥,技術不好,還請指教,如有更好的方法,還請告訴我,謝謝。

 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         public bool CustomCompareStrings(string useSetValue, string nowPageValue)
 8         {
 9             string[] values;
10             if (useSetValue != null)
11             {
12                 if (useSetValue.Contains(','))
13                 {
14                     values = useSetValue.ToLower().Split(',');
15                     foreach (string value in values)
16                     {
17                         if (value == nowPageValue.ToLower())
18                         {
19                             return true;
20                         }
21                     }
22                 }
23                 else
24                 {
25                     if (useSetValue.ToLower() == nowPageValue.ToLower())
26                     {
27                         return true;
28                     }
29                 }
30             }
31             return false;
32         }

好了,開始運用在自定義過濾器中了。

 1     /// <summary>
 2     /// 文件壓縮特性
 3     /// </summary>
 4     public class CompressFilterAttribute : ActionFilterAttribute
 5     {
 6         private const CompressionMode compress = CompressionMode.Compress;
 7 
 8         /// <summary>
 9         /// 構造函數
10         /// </summary>
11         public CompressFilterAttribute()
12         {
13         }
14 
15         /// <summary>
16         /// 執行壓縮方法
17         /// </summary>
18         /// <param name="filterContext"></param>
19         public override void OnActionExecuting(ActionExecutingContext filterContext)
20         {
28             if (IsRemoveFilter(filterContext))
29             {
30                 filterContext.HttpContext.Response.Write("該頁未執行GZIP壓縮");
31                 return;
32             }
33 
34             HttpRequestBase request = filterContext.HttpContext.Request;
35             HttpResponseBase response = filterContext.HttpContext.Response;
36             string acceptEncoding = request.Headers["Accept-Encoding"].ToLower();
37 
38             if (string.IsNullOrEmpty(acceptEncoding))
39             {
40                 return;
41             }
42 
43             if (acceptEncoding.Contains("gzip"))
44             {
45                 response.Filter = new GZipStream(response.Filter, compress);
46                 response.AppendHeader("Content-Encoding", "gzip");
47             }
48             else if (acceptEncoding.Contains("deflate"))
49             {
50                 response.Filter = new DeflateStream(response.Filter, compress);
51                 response.AppendHeader("Content-Encoding", "deflate");
52             }
53         }
54 
55     }

 

接下來就是將我們的過濾器註冊到全局過濾器中了。

1         /// <summary>
2         /// 註冊全局過濾器
3         /// </summary>
4         /// <param name="filter"></param>
5         public static void RegistrationGlobalFilters(GlobalFilterCollection filter)
6         {
7             filter.Add(new HandleErrorAttribute());
8             filter.Add(new CompressFilterAttribute("Home", "Index"));
9         }

最後,我們來看看效果吧。 

由於之前設置了,排除了Home控制器下的index方法,也就沒有對頁面進行GZIP壓縮了。 

這只是我自己想的一種方法,如果你有更好的方法,記得告訴我,謝謝。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章