public override void Because() { actionFilterRegistry = new ActionFilterRegistry(CreateStub<IFluentMvcObjectFactory>()); filterInstance = CreateStub<IActionFilter>(); FluentMvcConfiguration.Create(CreateStub<IFluentMvcResolver>(), actionFilterRegistry, CreateStub<IActionResultRegistry>(), CreateStub<IFilterConventionCollection>()) .WithFilter(filterInstance).BuildFilterProvider(); }
private static ActionExecutedContext InvokeActionFilter(IActionFilter filter, ActionExecutionContext context, Func<ActionExecutedContext> continuation) { filter.OnExecuting(context); if (context.Cancel) return new ActionExecutedContext(context, null) { Result = context.Result }; bool wasError = false; ActionExecutedContext postContext = null; try { postContext = continuation(); } catch (Exception ex) { wasError = true; postContext = new ActionExecutedContext(context, ex); filter.OnExecuted(postContext); if (!postContext.ExceptionHandled) throw; } if (!wasError) filter.OnExecuted(postContext); return postContext; }
/// <summary> /// Initializes a new instance of the <see cref="QueryFilterProvider" /> class. /// </summary> /// <param name="queryFilter">The action filter that executes the query.</param> public QueryFilterProvider(IActionFilter queryFilter) { if (queryFilter == null) { throw Error.ArgumentNull("queryFilter"); } QueryFilter = queryFilter; }
/// <summary> /// Enables query support for actions with an <see cref="IQueryable" /> or <see cref="IQueryable{T}" /> return type. /// </summary> /// <param name="configuration">The server configuration.</param> /// <param name="queryFilter">The action filter that executes the query.</param> public static void EnableQuerySupport(this HttpConfiguration configuration, IActionFilter queryFilter) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } configuration.Services.Add(typeof(IFilterProvider), new QueryFilterProvider(queryFilter)); }
/// <summary> /// Initializes a new instance of the <see cref="QueryFilterProvider" /> class. /// </summary> /// <param name="queryFilter">The action filter that executes the query.</param> public QueryFilterProvider(IActionFilter queryFilter) { if (queryFilter == null) { throw new ArgumentNullException("queryFilter"); } QueryFilter = queryFilter; }
public override void Given() { actionFilter = new TestActionFilter(); objectFactory = CreateStub<IFluentMvcObjectFactory>(); objectFactory.Stub(of => of.Resolve<IActionFilter>(Arg<Type>.Is.Anything)) .Return(actionFilter); actionFilterRegistry = new ActionFilterRegistry(objectFactory); actionFilterRegistryTester = new ActionFilterRegistryTester(actionFilterRegistry); }
internal QueryFilterProvider(IActionFilter queryFilter, bool skipQueryableAttribute) { if (queryFilter == null) { throw Error.ArgumentNull("queryFilter"); } QueryFilter = queryFilter; _skipQueryableAttribute = skipQueryableAttribute; }
/// <summary> /// Initializes a new instance of the <see cref="TableFilterProvider"/> using the provided <see cref="QueryableAttribute"/> /// implementation for executing the query. /// </summary> public TableFilterProvider(IActionFilter queryFilter) { if (queryFilter == null) { throw new ArgumentNullException("queryFilter"); } this.queryFilter = new QueryableAttribute() { PageSize = TableUtils.PageSize }; this.queryFilterProvider = new QueryFilterProvider(queryFilter); this.tableFilter = new FilterInfo(new TableQueryFilter(), FilterScope.Global); }
public void InvokeActionWithActionFilters_ChainsFiltersInOrderFollowedByInnerActionContinuation() { // Arrange HttpActionContext actionContextInstance = ContextUtil.CreateActionContext(); List<string> log = new List<string>(); Mock<IActionFilter> globalFilterMock = CreateActionFilterMock((ctx, ct, continuation) => { log.Add("globalFilter"); return continuation(); }); Mock<IActionFilter> actionFilterMock = CreateActionFilterMock((ctx, ct, continuation) => { log.Add("actionFilter"); return continuation(); }); Func<Task<HttpResponseMessage>> innerAction = () => Task<HttpResponseMessage>.Factory.StartNew(() => { log.Add("innerAction"); return null; }); var filters = new IActionFilter[] { globalFilterMock.Object, actionFilterMock.Object, }; // Act var result = ActionFilterResult.InvokeActionWithActionFilters(actionContextInstance, CancellationToken.None, filters, innerAction); // Assert Assert.NotNull(result); var resultTask = result(); Assert.NotNull(resultTask); resultTask.WaitUntilCompleted(); Assert.Equal(new[] { "globalFilter", "actionFilter", "innerAction" }, log.ToArray()); globalFilterMock.Verify(); actionFilterMock.Verify(); }
public static void AddActionFilter(this IServiceContainer container, IActionFilter filter) { container.AddFilter(new FilterInfo { Instance = filter, Order = int.MinValue }); }
/// <summary> /// 给过滤器排序 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> private int Compare( IActionFilter x, IActionFilter y ) { if (x.Order > y.Order) return 1; if (x.Order < y.Order) return -1; return 0; }
internal static Func<ActionExecutedContext> InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func<Func<ActionExecutedContext>> nextInChain) { filter.OnActionExecuting(preContext); if (preContext.Result != null) { ActionExecutedContext shortCircuitedPostContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, true /* canceled */, null /* exception */) { Result = preContext.Result }; return () => shortCircuitedPostContext; } // There is a nested try / catch block here that contains much the same logic as the outer block. // Since an exception can occur on either side of the asynchronous invocation, we need guards on // on both sides. In the code below, the second side is represented by the nested delegate. This // is really just a parallel of the synchronous ControllerActionInvoker.InvokeActionMethodFilter() // method. try { Func<ActionExecutedContext> continuation = nextInChain(); // add our own continuation, then return the new function return () => { ActionExecutedContext postContext; bool wasError = true; try { postContext = continuation(); wasError = false; } catch (ThreadAbortException) { // This type of exception occurs as a result of Response.Redirect(), but we special-case so that // the filters don't see this as an error. postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, null /* exception */); filter.OnActionExecuted(postContext); throw; } catch (Exception ex) { postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, ex); filter.OnActionExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } } if (!wasError) { filter.OnActionExecuted(postContext); } return postContext; }; } catch (ThreadAbortException) { // This type of exception occurs as a result of Response.Redirect(), but we special-case so that // the filters don't see this as an error. ActionExecutedContext postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, null /* exception */); filter.OnActionExecuted(postContext); throw; } catch (Exception ex) { ActionExecutedContext postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, ex); filter.OnActionExecuted(postContext); if (postContext.ExceptionHandled) { return () => postContext; } else { throw; } } }
/// <summary> /// Adds the specified filter to the ActionFilter list. /// </summary> /// <param name="filter"></param> protected virtual void AddActionFilter(IActionFilter filter) { if (ActionFilters.Contains(filter)) return; ActionFilters.Add(filter); }
public void Add(IActionFilter filter, int?order = null) { Add(filter, order); }
public void InvokeActionMethodWithFilters() { // Arrange List<string> actionLog = new List<string>(); ControllerContext controllerContext = new ControllerContext(); Dictionary<string, object> parameters = new Dictionary<string, object>(); MockAsyncResult innerAsyncResult = new MockAsyncResult(); ActionResult actionResult = new ViewResult(); ActionFilterImpl filter1 = new ActionFilterImpl() { OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) { actionLog.Add("OnActionExecuting1"); }, OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) { actionLog.Add("OnActionExecuted1"); } }; ActionFilterImpl filter2 = new ActionFilterImpl() { OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) { actionLog.Add("OnActionExecuting2"); }, OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) { actionLog.Add("OnActionExecuted2"); } }; Mock<AsyncActionDescriptor> mockActionDescriptor = new Mock<AsyncActionDescriptor>(); mockActionDescriptor.Setup(d => d.BeginExecute(controllerContext, parameters, It.IsAny<AsyncCallback>(), It.IsAny<object>())).Returns(innerAsyncResult); mockActionDescriptor.Setup(d => d.EndExecute(innerAsyncResult)).Returns(actionResult); AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker(); IActionFilter[] filters = new IActionFilter[] { filter1, filter2 }; // Act IAsyncResult outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null); ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult); // Assert Assert.Equal(new[] { "OnActionExecuting1", "OnActionExecuting2", "OnActionExecuted2", "OnActionExecuted1" }, actionLog.ToArray()); Assert.Equal(actionResult, postContext.Result); }
public void SetFilter(IActionFilter actionFilter) { _actionFilter = actionFilter; }
public ActionFilterOverride(IActionFilter wrappedFilter) { this._wrappedFilter = wrappedFilter; }
/// <summary> /// Initializes a new instance of the <see cref="AbstractActionFilter"/> class. /// </summary> protected AbstractActionFilter() { this.internalFilter = new InternalActionFilter(this); }
public GlimpseActionFilter(IActionFilter actionFilter) { ActionFilter = actionFilter; }
public static IEnumerable <IFilter> CreateFilterTracers(IFilter filter, ITraceWriter traceWriter) { List <IFilter> filters = new List <IFilter>(); bool addedActionAttributeTracer = false; bool addedAuthorizationAttributeTracer = false; bool addedExceptionAttributeTracer = false; ActionFilterAttribute actionFilterAttribute = filter as ActionFilterAttribute; if (actionFilterAttribute != null) { filters.Add(new ActionFilterAttributeTracer(actionFilterAttribute, traceWriter)); addedActionAttributeTracer = true; } AuthorizationFilterAttribute authorizationFilterAttribute = filter as AuthorizationFilterAttribute; if (authorizationFilterAttribute != null) { filters.Add(new AuthorizationFilterAttributeTracer(authorizationFilterAttribute, traceWriter)); addedAuthorizationAttributeTracer = true; } ExceptionFilterAttribute exceptionFilterAttribute = filter as ExceptionFilterAttribute; if (exceptionFilterAttribute != null) { filters.Add(new ExceptionFilterAttributeTracer(exceptionFilterAttribute, traceWriter)); addedExceptionAttributeTracer = true; } // Do not add an IActionFilter tracer if we already added an ActionFilterAttribute tracer IActionFilter actionFilter = filter as IActionFilter; if (actionFilter != null && !addedActionAttributeTracer) { filters.Add(new ActionFilterTracer(actionFilter, traceWriter)); } // Do not add an IAuthorizationFilter tracer if we already added an AuthorizationFilterAttribute tracer IAuthorizationFilter authorizationFilter = filter as IAuthorizationFilter; if (authorizationFilter != null && !addedAuthorizationAttributeTracer) { filters.Add(new AuthorizationFilterTracer(authorizationFilter, traceWriter)); } IAuthenticationFilter authenticationFilter = filter as IAuthenticationFilter; if (authenticationFilter != null) { filters.Add(new AuthenticationFilterTracer(authenticationFilter, traceWriter)); } // Do not add an IExceptionFilter tracer if we already added an ExceptoinFilterAttribute tracer IExceptionFilter exceptionFilter = filter as IExceptionFilter; if (exceptionFilter != null && !addedExceptionAttributeTracer) { filters.Add(new ExceptionFilterTracer(exceptionFilter, traceWriter)); } IOverrideFilter overrideFilter = filter as IOverrideFilter; if (overrideFilter != null) { filters.Add(new OverrideFilterTracer(overrideFilter, traceWriter)); } if (filters.Count == 0) { filters.Add(new FilterTracer(filter, traceWriter)); } return(filters); }
internal static Func <ActionExecutedContext> InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func <Func <ActionExecutedContext> > nextInChain) { filter.OnActionExecuting(preContext); if (preContext.Result != null) { ActionExecutedContext shortCircuitedPostContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, true /* canceled */, null /* exception */) { Result = preContext.Result }; return(() => shortCircuitedPostContext); } // There is a nested try / catch block here that contains much the same logic as the outer block. // Since an exception can occur on either side of the asynchronous invocation, we need guards on // on both sides. In the code below, the second side is represented by the nested delegate. This // is really just a parallel of the synchronous ControllerActionInvoker.InvokeActionMethodFilter() // method. try { Func <ActionExecutedContext> continuation = nextInChain(); // add our own continuation, then return the new function return(() => { ActionExecutedContext postContext; bool wasError = true; try { postContext = continuation(); wasError = false; } catch (ThreadAbortException) { // This type of exception occurs as a result of Response.Redirect(), but we special-case so that // the filters don't see this as an error. postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, null /* exception */); filter.OnActionExecuted(postContext); throw; } catch (Exception ex) { postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, ex); filter.OnActionExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } } if (!wasError) { filter.OnActionExecuted(postContext); } return postContext; }); } catch (ThreadAbortException) { // This type of exception occurs as a result of Response.Redirect(), but we special-case so that // the filters don't see this as an error. ActionExecutedContext postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, null /* exception */); filter.OnActionExecuted(postContext); throw; } catch (Exception ex) { ActionExecutedContext postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, ex); filter.OnActionExecuted(postContext); if (postContext.ExceptionHandled) { return(() => postContext); } else { throw; } } }
internal static ActionExecutedContext InvokeActionMethodFilter( IActionFilter filter, ActionExecutingContext preContext, Func <ActionExecutedContext> continuation ) { filter.OnActionExecuting(preContext); if (preContext.Result != null) { return(new ActionExecutedContext( preContext, preContext.ActionDescriptor, true /* canceled */ , null /* exception */ ) { Result = preContext.Result }); } bool wasError = false; ActionExecutedContext postContext = null; try { postContext = continuation(); } catch (ThreadAbortException) { // This type of exception occurs as a result of Response.Redirect(), but we special-case so that // the filters don't see this as an error. postContext = new ActionExecutedContext( preContext, preContext.ActionDescriptor, false /* canceled */ , null /* exception */ ); filter.OnActionExecuted(postContext); throw; } catch (Exception ex) { wasError = true; postContext = new ActionExecutedContext( preContext, preContext.ActionDescriptor, false /* canceled */ , ex ); filter.OnActionExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } } if (!wasError) { filter.OnActionExecuted(postContext); } return(postContext); }
/// <summary> /// Enables query support for actions with an <see cref="IQueryable" /> or <see cref="IQueryable{T}" /> return /// type. To avoid processing unexpected or malicious queries, use the validation settings on /// <see cref="EnableQueryAttribute"/> to validate incoming queries. For more information, visit /// http://go.microsoft.com/fwlink/?LinkId=279712. /// </summary> /// <param name="services">The services collection.</param> /// <param name="queryFilter">The action filter that executes the query.</param> public static IServiceCollection AddODataQueryFilter(this IServiceCollection services, IActionFilter queryFilter) { if (services == null) { throw Error.ArgumentNull("services"); } services.TryAddEnumerable(ServiceDescriptor.Singleton <IFilterProvider>(new QueryFilterProvider(queryFilter))); return(services); }
static ActionExecutedContext InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func<ActionExecutedContext> continuation) { filter.OnActionExecuting(preContext); if (preContext.Result != null) { return new ActionExecutedContext(preContext, true /* canceled */, null /* exception */) { Result = preContext.Result }; } bool wasError = false; ActionExecutedContext postContext = null; try { postContext = continuation(); } catch (Exception ex) { wasError = true; postContext = new ActionExecutedContext(preContext, false /* canceled */, ex); filter.OnActionExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } } if (!wasError) { if (!(postContext.Result is AsyncResult)) { filter.OnActionExecuted(postContext); } } return postContext; }
private static void BeforeOnActionExecutingImpl(DiagnosticListener diagnosticListener, ActionExecutingContext actionExecutingContext, IActionFilter filter) { if (diagnosticListener.IsEnabled(Diagnostics.BeforeActionFilterOnActionExecutingEventData.EventName)) { diagnosticListener.Write( Diagnostics.BeforeActionFilterOnActionExecutingEventData.EventName, new BeforeActionFilterOnActionExecutingEventData( actionExecutingContext.ActionDescriptor, actionExecutingContext, filter )); } }
public void AssertFilterIsNotReturned(IActionFilter actionFilter) { Assert.That(ReturnedFilters().Contains(actionFilter), Is.False, "Filter was not returned"); }
private static void AfterOnActionExecutedImpl(DiagnosticListener diagnosticListener, ActionExecutedContext actionExecutedContext, IActionFilter filter) { if (diagnosticListener.IsEnabled(Diagnostics.AfterActionFilterOnActionExecutedEventData.EventName)) { diagnosticListener.Write( Diagnostics.AfterActionFilterOnActionExecutedEventData.EventName, new AfterActionFilterOnActionExecutedEventData( actionExecutedContext.ActionDescriptor, actionExecutedContext, filter )); } }
public static void AfterOnActionExecuted( this DiagnosticSource diagnosticSource, ActionDescriptor actionDescriptor, ActionExecutedContext actionExecutedContext, IActionFilter filter) { if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.AfterOnActionExecuted")) { diagnosticSource.Write( "Microsoft.AspNetCore.Mvc.AfterOnActionExecuted", new { actionDescriptor = actionDescriptor, actionExecutedContext = actionExecutedContext, filter = filter }); } }
public LogFilter(IActionFilter preFilter, IActionFilter postFilter) { this.preFilter = preFilter; this.postFilter = postFilter; }
internal static IAsyncResult BeginInvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, BeginInvokeCallback beginContinuation, AsyncCallback<ActionExecutedContext> endContinuation, AsyncCallback callback, object state) { filter.OnActionExecuting(preContext); if (preContext.Result != null) { ActionExecutedContext shortCircuitContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, true /* canceled */, null /* exception */) { Result = preContext.Result }; return new ObjectAsyncResult<ActionExecutedContext>(shortCircuitContext).ToAsyncResultWrapper(callback, state); } try { return AsyncResultWrapper.Wrap(callback, state, beginContinuation, ar => BeginInvokeActionMethodFilterEndContinuation(filter, preContext, () => endContinuation(ar))); } catch (ThreadAbortException) { // This type of exception occurs as a result of Response.Redirect(), but we special-case so that // the filters don't see this as an error. ActionExecutedContext postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, null /* exception */); filter.OnActionExecuted(postContext); throw; } catch (Exception ex) { ActionExecutedContext postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, ex); filter.OnActionExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } return new ObjectAsyncResult<ActionExecutedContext>(postContext).ToAsyncResultWrapper(callback, state); } }
public void InvokeActionMethodWithFilters_ShortCircuited() { // Arrange List<string> actionLog = new List<string>(); ControllerContext controllerContext = new ControllerContext(); Dictionary<string, object> parameters = new Dictionary<string, object>(); ActionResult actionResult = new ViewResult(); ActionFilterImpl filter1 = new ActionFilterImpl() { OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) { actionLog.Add("OnActionExecuting1"); }, OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) { actionLog.Add("OnActionExecuted1"); } }; ActionFilterImpl filter2 = new ActionFilterImpl() { OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) { actionLog.Add("OnActionExecuting2"); filterContext.Result = actionResult; }, OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) { actionLog.Add("OnActionExecuted2"); } }; Mock<AsyncActionDescriptor> mockActionDescriptor = new Mock<AsyncActionDescriptor>(); mockActionDescriptor.Expect(d => d.BeginExecute(controllerContext, parameters, It.IsAny<AsyncCallback>(), It.IsAny<object>())).Throws(new Exception("I shouldn't have been called.")); mockActionDescriptor.Expect(d => d.EndExecute(It.IsAny<IAsyncResult>())).Throws(new Exception("I shouldn't have been called.")); AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker(); IActionFilter[] filters = new IActionFilter[] { filter1, filter2 }; // Act IAsyncResult outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null); ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult); // Assert CollectionAssert.AreEqual( new string[] { "OnActionExecuting1", "OnActionExecuting2", "OnActionExecuted1" }, actionLog); Assert.AreEqual(actionResult, postContext.Result); }
private ActionResult BeingInvokeActionMethodWithFiltersTester(Func<IAsyncResult> beginFunction, Func<ActionResult> endFunction, bool checkBegin, bool checkEnd, IActionFilter[] filters) { AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker(); ControllerContext controllerContext = new ControllerContext(); Dictionary<string, object> parameters = new Dictionary<string, object>(); Mock<AsyncActionDescriptor> mockActionDescriptor = new Mock<AsyncActionDescriptor>(); bool endExecuteCalled = false; bool beginExecuteCalled = false; Func<ActionResult> endExecute = () => { endExecuteCalled = true; return endFunction(); }; Func<IAsyncResult> beingExecute = () => { beginExecuteCalled = true; return beginFunction(); }; mockActionDescriptor.Setup(d => d.BeginExecute(controllerContext, parameters, It.IsAny<AsyncCallback>(), It.IsAny<object>())).Returns(beingExecute); mockActionDescriptor.Setup(d => d.EndExecute(It.IsAny<IAsyncResult>())).Returns(endExecute); IAsyncResult outerAsyncResult = null; try { outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null); } catch (Exception ex) { if (checkEnd) { // Testing end, so not expecting exception thrown from begin Assert.NotNull(ex); } else { throw ex; } } Assert.NotNull(outerAsyncResult); Assert.Equal(checkBegin, beginExecuteCalled); Assert.False(endExecuteCalled); ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult); Assert.NotNull(postContext); if (checkEnd) { Assert.True(endExecuteCalled); } return postContext.Result; }
/// <summary> /// Initializes a new instance of the <see cref="QueryFilterProvider" /> class. /// </summary> /// <param name="queryFilter">The action filter that executes the query.</param> public QueryFilterProvider(IActionFilter queryFilter) : this(queryFilter, skipQueryableAttribute: false) { }
public ModelValidationFilterTests() { _filter = new ModelValidationFilter(); }
public HeaderFilter(IActionFilter preFilter, IActionFilter postFilter) { this.preFilter = preFilter; this.postFilter = postFilter; }
internal Func <ActionExecutedContext> InvokeActionMethodFilterAsynchronouslyRecursive(int filterIndex) { // Performance-sensitive // For compatability, the following behavior must be maintained // The OnActionExecuting events must fire in forward order // The Begin and End events must fire // The OnActionExecuted events must fire in reverse order // Earlier filters can process the results and exceptions from the handling of later filters // This is achieved by calling recursively and moving through the filter list forwards // If there are no more filters to recurse over, create the main result if (filterIndex > _filterCount - 1) { InnerAsyncResult = _invoker.BeginInvokeActionMethod(_controllerContext, _actionDescriptor, _parameters, _asyncCallback, _asyncState); return(() => new ActionExecutedContext(_controllerContext, _actionDescriptor, canceled: false, exception: null) { Result = _invoker.EndInvokeActionMethod(InnerAsyncResult) }); } // Otherwise process the filters recursively IActionFilter filter = _filters[filterIndex]; ActionExecutingContext preContext = _preContext; filter.OnActionExecuting(preContext); if (preContext.Result != null) { ActionExecutedContext shortCircuitedPostContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, canceled: true, exception: null) { Result = preContext.Result }; return(() => shortCircuitedPostContext); } // There is a nested try / catch block here that contains much the same logic as the outer block. // Since an exception can occur on either side of the asynchronous invocation, we need guards on // on both sides. In the code below, the second side is represented by the nested delegate. This // is really just a parallel of the synchronous ControllerActionInvoker.InvokeActionMethodFilter() // method. try { // Use the filters in forward direction int nextFilterIndex = filterIndex + 1; Func <ActionExecutedContext> continuation = InvokeActionMethodFilterAsynchronouslyRecursive(nextFilterIndex); // add our own continuation, then return the new function return(() => { ActionExecutedContext postContext; bool wasError = true; try { postContext = continuation(); wasError = false; } catch (ThreadAbortException) { // This type of exception occurs as a result of Response.Redirect(), but we special-case so that // the filters don't see this as an error. postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, canceled: false, exception: null); filter.OnActionExecuted(postContext); throw; } catch (Exception ex) { postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, canceled: false, exception: ex); filter.OnActionExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } } if (!wasError) { filter.OnActionExecuted(postContext); } return postContext; }); } catch (ThreadAbortException) { // This type of exception occurs as a result of Response.Redirect(), but we special-case so that // the filters don't see this as an error. ActionExecutedContext postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, canceled: false, exception: null); filter.OnActionExecuted(postContext); throw; } catch (Exception ex) { ActionExecutedContext postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, canceled: false, exception: ex); filter.OnActionExecuted(postContext); if (postContext.ExceptionHandled) { return(() => postContext); } else { throw; } } }
internal static ActionExecutedContext InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func<ActionExecutedContext> continuation) { filter.OnActionExecuting(preContext); if (preContext.Result != null) { return new ActionExecutedContext(preContext, preContext.ActionDescriptor, true /* canceled */, null /* exception */) { Result = preContext.Result }; } bool wasError = false; ActionExecutedContext postContext = null; try { postContext = continuation(); } catch (ThreadAbortException) { // This type of exception occurs as a result of Response.Redirect(), but we special-case so that // the filters don't see this as an error. postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, null /* exception */); filter.OnActionExecuted(postContext); throw; } catch (Exception ex) { wasError = true; postContext = new ActionExecutedContext(preContext, preContext.ActionDescriptor, false /* canceled */, ex); filter.OnActionExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } } if (!wasError) { filter.OnActionExecuted(postContext); } return postContext; }
public static void BeforeOnActionExecuting( this DiagnosticSource diagnosticSource, ActionExecutingContext actionExecutingContext, IActionFilter filter) { Debug.Assert(diagnosticSource != null); Debug.Assert(actionExecutingContext != null); Debug.Assert(filter != null); if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.BeforeOnActionExecuting")) { diagnosticSource.Write( "Microsoft.AspNetCore.Mvc.BeforeOnActionExecuting", new { actionDescriptor = actionExecutingContext.ActionDescriptor, actionExecutingContext = actionExecutingContext, filter = filter }); } }
public static void EnableQuerySupport(this HttpConfiguration configuration, IActionFilter queryFilter) { Extensions.HttpConfigurationExtensions.AddODataQueryFilter(configuration, queryFilter); }
public ActionFilterTracer(IActionFilter innerFilter, ITraceWriter traceWriter) : base(innerFilter, traceWriter) { }
public ActionFilterOverride(IActionFilter wrappedFilter) { _wrappedFilter = wrappedFilter; }
protected static Func<ActionExecutedContext> InvokeActionFilterAsynchronously(IActionFilter filter, ActionExecutionContext preContext, Func<Func<ActionExecutedContext>> next) { filter.OnExecuting(preContext); if (preContext.Result != null) { ActionExecutedContext shortCircuitedPostContext = new ActionExecutedContext(preContext, null) { Result = preContext.Result }; return () => shortCircuitedPostContext; } try { Func<ActionExecutedContext> continuation = next(); return () => { ActionExecutedContext postContext; bool wasError = true; try { postContext = continuation(); wasError = false; } catch (ThreadAbortException) { postContext = new ActionExecutedContext(preContext, null); filter.OnExecuted(postContext); throw; } catch (Exception ex) { postContext = new ActionExecutedContext(preContext, ex); filter.OnExecuted(postContext); if (!postContext.ExceptionHandled) throw; } if (!wasError) filter.OnExecuted(postContext); return postContext; }; } catch (ThreadAbortException) { ActionExecutedContext postContext = new ActionExecutedContext(preContext, null); filter.OnExecuted(postContext); throw; } catch (Exception ex) { ActionExecutedContext postContext = new ActionExecutedContext(preContext, ex); filter.OnExecuted(postContext); if (postContext.ExceptionHandled) return () => postContext; throw; } }