private ResultExecutedContext InvokeActionResultFilterRecursive(IList <IResultFilter> filters, int filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) { // Performance-sensitive // For compatbility, the following behavior must be maintained // The OnResultExecuting events must fire in forward order // The InvokeActionResult must then fire // The OnResultExecuted 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 > filters.Count - 1) { InvokeActionResult(controllerContext, actionResult); return(new ResultExecutedContext(controllerContext, actionResult, canceled: false, exception: null)); } // Otherwise process the filters recursively IResultFilter filter = filters[filterIndex]; filter.OnResultExecuting(preContext); if (preContext.Cancel) { return(new ResultExecutedContext(preContext, preContext.Result, canceled: true, exception: null)); } bool wasError = false; ResultExecutedContext postContext = null; try { // Use the filters in forward direction int nextFilterIndex = filterIndex + 1; postContext = InvokeActionResultFilterRecursive(filters, nextFilterIndex, preContext, controllerContext, actionResult); } 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 ResultExecutedContext(preContext, preContext.Result, canceled: false, exception: null); filter.OnResultExecuted(postContext); throw; } catch (Exception ex) { wasError = true; postContext = new ResultExecutedContext(preContext, preContext.Result, canceled: false, exception: ex); filter.OnResultExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } } if (!wasError) { filter.OnResultExecuted(postContext); } return(postContext); }
private static ResultExecutedContext InvokeActionResultFilter(IResultFilter filter, ResultExecutionContext context, Func <ResultExecutedContext> continuation) { filter.OnResultExecuting(context); if (context.Cancel) { return(new ResultExecutedContext(context, null)); } bool wasError = false; ResultExecutedContext 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 ResultExecutedContext(context, null); filter.OnResultExecuted(postContext); throw; } catch (Exception ex) { wasError = true; postContext = new ResultExecutedContext(context, ex); filter.OnResultExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } } if (!wasError) { filter.OnResultExecuted(postContext); } return(postContext); }
private static async Task ExecuteSyncFilter( ResultExecutingContext context, ResultExecutionDelegate next, IResultFilter resultFilter) { resultFilter.OnResultExecuting(context); if (!context.Cancel) { resultFilter.OnResultExecuted(await next()); } }
/// <summary> /// Execute the prefilter result, the actionResult and the postfilter result /// </summary> /// <param name="filter">The result filter</param> /// <param name="preContext">The builded preContext</param> /// <param name="continuation">The lambda expression wich execute the actionResult and the post filter result attribute</param> /// <returns>The result of the execution of the post filter result attribute</returns> private ResultExecutedContext InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func <ResultExecutedContext> continuation) { //Executing the filter filter.OnResultExecuting(preContext); //If the execution is cancelled if (preContext.Cancel) { return(new ResultExecutedContext(preContext.Controller, preContext.Result, true /* canceled */, null /* exception */)); } bool wasError = false; ResultExecutedContext postContext = null; try { //Creating the ResultExecutedContext by executing the actionResult postContext = continuation(); } catch (Exception ex) { wasError = true; //Creating the context postContext = new ResultExecutedContext(preContext.Controller, preContext.Result, false /* canceled */, ex); filter.OnResultExecuted(postContext); //If the exception is !handled we throw it if (!postContext.ExceptionHandled) { throw; } } //Everything is executed well if (!wasError) { filter.OnResultExecuted(postContext); } return(postContext); }
internal static ModuleResultExecutedContext InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func <ModuleResultExecutedContext> continuation) { filter.OnResultExecuting(preContext); if (preContext.Cancel) { return(new ModuleResultExecutedContext(preContext, preContext.Result, true /* canceled */, null /* exception */, "")); } bool wasError = false; ModuleResultExecutedContext 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 ModuleResultExecutedContext(preContext, preContext.Result, false /* canceled */, null /* exception */, ""); filter.OnResultExecuted(postContext); throw; } catch (Exception ex) { wasError = true; postContext = new ModuleResultExecutedContext(preContext, preContext.Result, false /* canceled */, ex, ""); filter.OnResultExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } } if (!wasError) { filter.OnResultExecuted(postContext); } return(postContext); }
/// <summary> /// 执行请求结果的筛选器 /// </summary> /// <param name="filter"></param> /// <param name="preContext"></param> /// <param name="continuation"></param> /// <returns></returns> internal static ResultExecutedContext InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func <ResultExecutedContext> continuation) { filter.OnResultExecuting(preContext); if (preContext.Cancel) { return(new ResultExecutedContext(preContext, preContext.Result, true, null)); } bool flag = false; ResultExecutedContext resultExecutedContext = null; try { resultExecutedContext = continuation(); } catch (ThreadAbortException) { resultExecutedContext = new ResultExecutedContext(preContext, preContext.Result, false, null); filter.OnResultExecuted(resultExecutedContext); throw; } catch (Exception exception) { flag = true; resultExecutedContext = new ResultExecutedContext(preContext, preContext.Result, false, exception); filter.OnResultExecuted(resultExecutedContext); if (!resultExecutedContext.ExceptionHandled) { throw; } } if (!flag) { filter.OnResultExecuted(resultExecutedContext); } return(resultExecutedContext); }
void ProcessExceptionWhenInvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Exception ex) { //Creating the context var postContext = new ResultExecutedContext(preContext.Result, false /* canceled */, ex) { Controller = preContext.Controller, ActionParameters = preContext.ActionParameters, ActionMethod = _actionMethod }; filter.OnResultExecuted(postContext); //If the exception is !handled we throw it if (!postContext.ExceptionHandled) { throw ex; } }
/// <summary> /// Execute the prefilter result, the actionResult and the postfilter result /// </summary> /// <param name="filter">The result filter</param> /// <param name="preContext">The builded preContext</param> /// <param name="continuation">The lambda expression wich execute the actionResult and the post filter result attribute</param> /// <returns>The result of the execution of the post filter result attribute</returns> ResultExecutedContext InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Delegates.Func <ResultExecutedContext> continuation) { //Executing the filter filter.OnResultExecuting(preContext); //If the execution is cancelled if (preContext.Cancel) { return(new ResultExecutedContext(preContext.Result, true /* canceled */, null /* exception */) { Controller = preContext.Controller, ActionParameters = preContext.ActionParameters, ActionMethod = _actionMethod }); } bool wasError = false; ResultExecutedContext postContext = null; try { //Creating the ResultExecutedContext by executing the actionResult postContext = continuation(); } catch (TargetInvocationException ex) { wasError = true; ProcessExceptionWhenInvokeActionResultFilter(filter, preContext, ex.InnerException ?? ex); } catch (Exception ex) { wasError = true; ProcessExceptionWhenInvokeActionResultFilter(filter, preContext, ex); } //Everything is executed well if (!wasError) { filter.OnResultExecuted(postContext); } return(postContext); }
internal static ResultExecutedContext InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func<ResultExecutedContext> continuation) { filter.OnResultExecuting(preContext); if (preContext.Cancel) { return new ResultExecutedContext(preContext, preContext.Result, true /* canceled */, null /* exception */); } bool wasError = false; ResultExecutedContext 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 ResultExecutedContext(preContext, preContext.Result, false /* canceled */, null /* exception */); filter.OnResultExecuted(postContext); throw; } catch (Exception ex) { wasError = true; postContext = new ResultExecutedContext(preContext, preContext.Result, false /* canceled */, ex); filter.OnResultExecuted(postContext); if (!postContext.ExceptionHandled) { throw; } } if (!wasError) { filter.OnResultExecuted(postContext); } return postContext; }
public void OnResultExecuted(ResultExecutedContext filterContext) { _wrappedFilter.OnResultExecuted(filterContext); }
private static ResultExecutedContext InvokeActionResultFilter(IResultFilter filter, ResultExecutionContext context, Func<ResultExecutedContext> continuation) { filter.OnResultExecuting(context); if (context.Cancel) return new ResultExecutedContext(context, null); bool wasError = false; ResultExecutedContext 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 ResultExecutedContext(context, null); filter.OnResultExecuted(postContext); throw; } catch (Exception ex) { wasError = true; postContext = new ResultExecutedContext(context, ex); filter.OnResultExecuted(postContext); if (!postContext.ExceptionHandled) throw; } if (!wasError) filter.OnResultExecuted(postContext); return postContext; }