/// <summary> /// Default action for all controllers /// </summary> /// <returns>The action execution result</returns> public virtual IActionResult Execute(ActionParameters parameters) { var NotImplementedMethodException = "The method should be implemented"; LoggingService.Error(NotImplementedMethodException); throw new NotImplementedException(NotImplementedMethodException); }
public IActionResult Invoke(IController controller, string actionName, ActionParameters parameters) { if (string.IsNullOrEmpty(actionName)) { actionName = "Execute"; } return((IActionResult)controller.GetType().GetMethod(actionName).Invoke(controller, new[] { parameters })); }
/// <summary> /// Constructor. The controller name and action name are injected using constructor injection /// </summary> /// <param name="controllerName">The name of the controller</param> /// <param name="actionName">The name of the actio</param> public RedirectToActionResult(string controllerName, string actionName, ActionParameters parameters) { this._controllerName = controllerName; this._actionName = actionName; this._parameters = parameters; }
/// <summary> /// Invoke an action /// </summary> /// <param name="controllerName">The controller's name</param> /// <param name="actionName">The action's name</param> public void Invoke(string controllerName, string actionName, ActionParameters parameters) { IController controller = null; IActionResult actionResult = null; if (controllerName == null) { //Default controller is invoked when null is passed as an argument controllerName = _defaultController; } //Getting the controller to invoke controller = ControllerFactory.CreateController(controllerName); try { //if null is passed as an argument then the default Execute action is executed if (string.IsNullOrEmpty(actionName)) { actionName = "Execute"; } //Getting the action mode bool asynchMode = controller.IsAnAsynchronousAction(actionName); //Retrieving the MethodInfo MethodInfo actionMethod = controller.GetType().GetMethod(actionName); //Retrieving filterInfo by applying the GetFilters extended method located in Extensions/CustomExtension actionFilterInfo = actionMethod.GetFilters(); //Invoking Autorization filters AuthorizationContext authContext = InvokeAuthorizationFilters(controller, actionFilterInfo.AuthorizationFilters); if (authContext != null) { if (authContext.Result != null) { InvokeActionResult(authContext.Result); return; } } //Executing the action ActionExecutedContext postActionContext = InvokeActionMethodWithFilters(controller, actionFilterInfo.ActionFilters, actionMethod, asynchMode, parameters);// (controllerContext, filterInfo.ActionFilters, actionDescriptor, parameters); if (postActionContext.Result == null) { return; } //Executing the actionResultFilter ResultExecutedContext postResultContext = InvokeActionResultWithFilters(controller, actionFilterInfo.ResultFilters, postActionContext.Result); } catch (Exception ex) { LoggingService.Error(ex); //Executing the exception filter //ExceptionContext exceptionContext = InvokeExceptionFilters(controller, actionFilterInfo.ExceptionFilters, ex.InnerException); //if (!exceptionContext.ExceptionHandled) //{ var ActionExecutionException = "An error occured while executing the action [{0}], check the underlying implementation : TECHNICAL_DETAIL [{1}] ."; //throw new Exception(string.Format(ActionExecutionException, actionName, ex.Message)); throw ex; // } // InvokeActionResult(exceptionContext.Result); } }
/// <summary> /// Launch the execution of filters and action method /// </summary> /// <param name="controller">The controller</param> /// <param name="filters">The list of action filters</param> /// <param name="action">The action to executed</param> /// <param name="asyncMode">The mode of the execution of the action</param> /// <returns>The actionExecutedContext build by the execution</returns> protected virtual ActionExecutedContext InvokeActionMethodWithFilters(IController controller, IList <IActionFilter> filters, MethodInfo action, bool asyncMode, ActionParameters parameters) { ActionExecutingContext preContext = new ActionExecutingContext(controller); //If there is no ActionFilter, the action is simple executed if (filters == null) { ActionExecutedContext postContext = new ActionExecutedContext(controller, false, null); postContext.Result = InvokeActionMethod(controller, action.Name, asyncMode, parameters); return(postContext); } //Now we know that there is, at least one actionFilter if (listOfExecutedActionFilters == null) { listOfExecutedActionFilters = new List <IActionFilter>(); } else { listOfExecutedActionFilters.Clear(); } //Passing the list throught the lambda expression Func <ActionExecutedContext> continuation = () => new ActionExecutedContext(controller, false, null) { Result = InvokeActionMethod(controller, action.Name, asyncMode, parameters) }; // need to reverse the filter list because the continuations are built up backward Func <ActionExecutedContext> reverseFiltersList = filters.Reverse().Aggregate(continuation, (next, filter) => () => InvokeActionMethodFilter(filter, preContext, next)); return(reverseFiltersList()); }
/// <summary> /// Invoke an action of a specific controller /// </summary> /// <param name="controller">The controller</param> /// <param name="actionName">The name of the action to execute</param> /// <param name="asyncMode">Indicates wheter the action should be executed in a asynchronous mode or not</param> /// <returns>The result of the action. Null is returned for asynchronous actions</returns> private IActionResult InvokeActionMethod(IController controller, string actionName, bool asyncMode, ActionParameters parameters) { //If the action should be executed asynchronously if (asyncMode && !this.BgAsyncMethods.IsBusy) { //building parameter parameters.Add("controller", controller); parameters.Add("actionName", actionName); BgAsyncMethods.RunWorkerAsync(parameters); return(null); } return(new ActionInvoker().Invoke(controller, actionName, parameters)); }
/// <summary> /// Intialize the action result to Execute another action /// </summary> /// <param name="controllerName">The controller name</param> /// <param name="methodName">The action to execute</param> /// <returns>The configured redirect action result</returns> public IActionResult RedirectToAction(string controllerName, string actionName, ActionParameters parameters) { return(new RedirectToActionResult(controllerName, actionName, parameters)); }