public virtual void ProvideFilter(FilterProviderContext context, FilterItem filterItem) { if (filterItem.Filter != null) { return; } var filter = filterItem.Descriptor.Filter; var filterFactory = filter as IFilterFactory; if (filterFactory == null) { filterItem.Filter = filter; } else { var services = context.ActionContext.HttpContext.RequestServices; filterItem.Filter = filterFactory.CreateInstance(services); if (filterItem.Filter == null) { throw new InvalidOperationException(Resources.FormatTypeMethodMustReturnNotNullValue( "CreateInstance", typeof(IFilterFactory).Name)); } ApplyFilterToContainer(filterItem.Filter, filterFactory); } }
/// <inheritdoc /> public void OnProvidersExecuting(FilterProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var services = context.ActionContext.HttpContext.RequestServices; var filters = services.GetService<IEnumerable<IFilterMetadata>>(); if (context.ActionContext.ActionDescriptor.FilterDescriptors != null) { foreach (var filter in filters) { var filterItem = new FilterItem(new FilterDescriptor(filter, FilterScope.Global), filter); context.Results.Add(filterItem); } } }
private void InsertControllerAsFilter(FilterProviderContext context, IFilter controllerFilter) { var descriptor = new FilterDescriptor(controllerFilter, FilterScope.Controller); var item = new FilterItem(descriptor, controllerFilter); // BinarySearch will return the index of where the item _should_be_ in the list. // // If index > 0: // Other items in the list have the same order and scope - the item was 'found'. // // If index < 0: // No other items in the list have the same order and scope - the item was not 'found' // Index will be the bitwise compliment of of the 'right' location. var index = context.Results.BinarySearch(item, FilterItemOrderComparer.Comparer); if (index < 0) { index = ~index; } context.Results.Insert(index, item); }
private TestControllerActionInvoker CreateInvoker( IFilterMetadata[] filters, bool actionThrows = false, int maxAllowedErrorsInModelState = 200) { var actionDescriptor = new ControllerActionDescriptor() { FilterDescriptors = new List<FilterDescriptor>(), Parameters = new List<ParameterDescriptor>(), }; if (actionThrows) { actionDescriptor.MethodInfo = typeof(ControllerActionInvokerTest).GetMethod("ThrowingActionMethod"); } else { actionDescriptor.MethodInfo = typeof(ControllerActionInvokerTest).GetMethod("ActionMethod"); } var httpContext = new Mock<HttpContext>(MockBehavior.Loose); var http = GetHttpContext(); var httpRequest = http.Request; var httpResponse = http.Response; httpContext.SetupGet(c => c.Request).Returns(httpRequest); httpContext.SetupGet(c => c.Response).Returns(httpResponse); httpContext .Setup(o => o.RequestServices.GetService(typeof(ILoggerFactory))) .Returns(NullLoggerFactory.Instance); httpResponse.Body = new MemoryStream(); var formatter = new Mock<IOutputFormatter>(); formatter .Setup(f => f.CanWriteResult(It.IsAny<OutputFormatterCanWriteContext>())) .Returns(true); formatter .Setup(f => f.WriteAsync(It.IsAny<OutputFormatterWriteContext>())) .Returns<OutputFormatterWriteContext>(async c => { await c.HttpContext.Response.WriteAsync(c.Object.ToString()); }); var options = new MvcOptions(); options.OutputFormatters.Add(formatter.Object); var optionsAccessor = new Mock<IOptions<MvcOptions>>(); optionsAccessor .SetupGet(o => o.Value) .Returns(options); httpContext .Setup(o => o.RequestServices.GetService(typeof(IOptions<MvcOptions>))) .Returns(optionsAccessor.Object); var actionContext = new ActionContext( httpContext: httpContext.Object, routeData: new RouteData(), actionDescriptor: actionDescriptor); var filterProvider = new Mock<IFilterProvider>(MockBehavior.Strict); filterProvider .Setup(fp => fp.OnProvidersExecuting(It.IsAny<FilterProviderContext>())) .Callback<FilterProviderContext>(context => { foreach (var filterMetadata in filters) { var filter = new FilterItem( new FilterDescriptor(filterMetadata, FilterScope.Action), filterMetadata); context.Results.Add(filter); } }); filterProvider .Setup(fp => fp.OnProvidersExecuted(It.IsAny<FilterProviderContext>())) .Verifiable(); var actionArgumentsBinder = new Mock<IControllerActionArgumentBinder>(); actionArgumentsBinder.Setup( b => b.BindActionArgumentsAsync(It.IsAny<ControllerContext>(), It.IsAny<object>())) .Returns(Task.FromResult<IDictionary<string, object>>(new Dictionary<string, object>())); filterProvider .SetupGet(fp => fp.Order) .Returns(-1000); var invoker = new TestControllerActionInvoker( actionContext, new[] { filterProvider.Object }, new MockControllerFactory(this), actionDescriptor, new IInputFormatter[0], actionArgumentsBinder.Object, new IModelBinder[0], new IModelValidatorProvider[0], new IValueProviderFactory[0], new NullLoggerFactory().CreateLogger<ControllerActionInvoker>(), new DiagnosticListener("Microsoft.AspNet"), maxAllowedErrorsInModelState); return invoker; }