From 1783a06dc0d61596ad6fca596c0b922dd42fc6f3 Mon Sep 17 00:00:00 2001 From: Yishai Galatzer Date: Mon, 3 Mar 2014 01:49:55 -0800 Subject: [PATCH] Filter discovery --- .../ReflectedActionDescriptorProvider.cs | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/ReflectedActionDescriptorProvider.cs b/src/Microsoft.AspNet.Mvc.Core/ReflectedActionDescriptorProvider.cs index 22253e9e99..604331eb58 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ReflectedActionDescriptorProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ReflectedActionDescriptorProvider.cs @@ -43,8 +43,8 @@ namespace Microsoft.AspNet.Mvc foreach (var cd in controllerDescriptors) { - var controllerAttributes = cd.ControllerTypeInfo.GetCustomAttributes(inherit: true).ToArray(); - var filtersFromController = GetOrderedFilterAttributes(controllerAttributes); + var controllerFilters = GetOrderedFilterAttributes(cd.ControllerTypeInfo); + bool allowAnonymous = IsAnonymous(controllerAttributes); @@ -59,24 +59,24 @@ namespace Microsoft.AspNet.Mvc foreach (var actionInfo in actionInfos) { - yield return BuildDescriptor(cd, methodInfo, actionInfo, filtersFromController, allowAnonymous); + yield return BuildDescriptor(cd, methodInfo, actionInfo, controllerFilters); } } } } - private bool IsAnonymous(object[] attributes) - { - return attributes.OfType().Any(); - } - - private IFilter[] GetOrderedFilterAttributes(object[] attributes) + private IFilter[] GetOrderedFilterAttributes(MemberInfo memberInfo) { + var attributes = memberInfo.GetCustomAttributes(inherit: true); var filters = attributes.OfType().OrderByDescending(filter => filter.Order); return filters.ToArray(); } + private ReflectedActionDescriptor BuildDescriptor(ControllerDescriptor controllerDescriptor, + MethodInfo methodInfo, + ActionInfo actionInfo, + IFilter[] controllerFilters) private ReflectedActionDescriptor BuildDescriptor(ControllerDescriptor controllerDescriptor, MethodInfo methodInfo, ActionInfo actionInfo, @@ -115,10 +115,10 @@ namespace Microsoft.AspNet.Mvc ad.Parameters = methodInfo.GetParameters().Select(p => _parameterDescriptorFactory.GetDescriptor(p)).ToList(); - var attributes = methodInfo.GetCustomAttributes(inherit: true).ToArray(); - // TODO: add ordering support such that action filters are ahead of controller filters if they have the same order - var filtersFromAction = GetOrderedFilterAttributes(attributes); + var actionFilters = GetOrderedFilterAttributes(methodInfo); + + ad.Filters = MergeSorted(actionFilters, controllerFilters); ad.Filters = MergeSorted(filtersFromAction, controllerFilters); @@ -127,29 +127,29 @@ namespace Microsoft.AspNet.Mvc return ad; } - private List MergeSorted(IFilter[] filtersFromAction, IFilter[] filtersFromController) + internal List MergeSorted(IFilter[] actionFilters, IFilter[] controllerFilters) { var list = new List(); - var count = filtersFromAction.Length + filtersFromController.Length; + var count = actionFilters.Length + controllerFilters.Length; for (int i = 0, j = 0; i + j < count; ) { - if (i >= filtersFromAction.Length) + if (i >= actionFilters.Length) { - list.Add(filtersFromController[j++]); + list.Add(controllerFilters[j++]); } - else if (j >= filtersFromController.Length) + else if (j >= controllerFilters.Length) { - list.Add(filtersFromAction[i++]); + list.Add(actionFilters[i++]); } - else if (filtersFromAction[i].Order >= filtersFromController[j].Order) + else if (actionFilters[i].Order >= controllerFilters[j].Order) { - list.Add(filtersFromAction[i++]); + list.Add(actionFilters[i++]); } else { - list.Add(filtersFromController[j++]); + list.Add(controllerFilters[j++]); } }