protected override void OnCommandException(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex) { ((ActionFilterErrorHandlingViewModel) context.ViewModel).Result = "error was handled"; context.IsCommandExceptionHandled = true; base.OnCommandException(context, actionInfo, ex); }
/// <summary> /// Called after the command is invoked. /// </summary> protected internal override void OnCommandExecuted(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception) { if (exception != null) { OnCommandException(context, actionInfo, exception); } }
/// <summary> /// Called before the command is executed. /// </summary> protected internal override void OnCommandExecuting(DotvvmRequestContext context, ActionInfo actionInfo) { if (!string.IsNullOrEmpty(context.ModelState.ValidationTargetPath)) { // perform the validation context.ModelState.Errors.AddRange(viewModelValidator.ValidateViewModel(context.ModelState.ValidationTarget)); // return the model state when error occurs context.FailOnInvalidModelState(); } base.OnCommandExecuting(context, actionInfo); }
protected virtual Task OnCommandExceptionAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex) => TaskUtils.GetCompletedTask();
/// <summary> /// Called when the exception occurs during the command invocation. /// </summary> protected virtual Task OnCommandExceptionAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex) => Task.FromResult(0);
/// <inheritdoc /> protected internal override Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo) { if (!string.IsNullOrEmpty(context.ModelState.ValidationTargetPath)) { var validator = context.Configuration.ServiceLocator.GetService <IViewModelValidator>(); context.ModelState.Errors.AddRange(validator.ValidateViewModel(context.ModelState.ValidationTarget)); context.FailOnInvalidModelState(); } return(Task.FromResult(0)); }
private IEnumerable <IAuthorizeData> GetAuthorizeData(ActionInfo actionInfo) => actionInfo?.Binding?.ActionFilters != null?actionInfo.Binding.ActionFilters.OfType <IAuthorizeData>().Select(WrapInAuthorizeAttribute) : Enumerable.Empty <IAuthorizeData>();
public async Task ProcessStaticCommandRequest(DotvvmRequestContext context) { JObject postData; using (var jsonReader = new JsonTextReader(new StreamReader(context.OwinContext.Request.Body))) { postData = JObject.Load(jsonReader); } // validate csrf token context.CsrfToken = postData["$csrfToken"].Value<string>(); CsrfProtector.VerifyToken(context, context.CsrfToken); var command = postData["command"].Value<string>(); var arguments = postData["args"] as JArray; var lastDot = command.LastIndexOf('.'); var typeName = command.Remove(lastDot); var methodName = command.Substring(lastDot + 1); var methodInfo = Type.GetType(typeName).GetMethod(methodName); if (!Attribute.IsDefined(methodInfo, typeof(AllowStaticCommandAttribute))) { throw new DotvvmHttpException($"This method cannot be called from the static command. If you need to call this method, add the '{nameof(AllowStaticCommandAttribute)}' to the method."); } var target = methodInfo.IsStatic ? null : arguments[0].ToObject(methodInfo.DeclaringType); var methodArguments = arguments.Skip(methodInfo.IsStatic ? 0 : 1) .Zip(methodInfo.GetParameters(), (arg, parameter) => arg.ToObject(parameter.ParameterType)) .ToArray(); var actionInfo = new ActionInfo() { IsControlCommand = false, Action = () => methodInfo.Invoke(target, methodArguments) }; var filters = context.Configuration.Runtime.GlobalFilters .Concat(methodInfo.DeclaringType.GetCustomAttributes<ActionFilterAttribute>()) .Concat(methodInfo.GetCustomAttributes<ActionFilterAttribute>()) .ToArray(); var task = ExecuteCommand(actionInfo, context, filters); await task; object result = TaskUtils.GetResult(task); using (var writer = new StreamWriter(context.OwinContext.Response.Body)) { writer.WriteLine(JsonConvert.SerializeObject(result)); } }
/// <summary> /// Called before the command is executed. /// </summary> protected internal override void OnCommandExecuting(IDotvvmRequestContext context, ActionInfo actionInfo) { if (!string.IsNullOrEmpty(context.ModelState.ValidationTargetPath)) { // perform the validation context.ModelState.Errors.AddRange(viewModelValidator.ValidateViewModel(context.ModelState.ValidationTarget)); // return the model state when error occurs context.FailOnInvalidModelState(); } base.OnCommandExecuting(context, actionInfo); }
Task ICommandActionFilter.OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception?exception) => OnCommandExecutedAsync(context, actionInfo, exception);
Task ICommandActionFilter.OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo) => OnCommandExecutingAsync(context, actionInfo);
protected internal virtual Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception?exception) => TaskUtils.GetCompletedTask();
protected internal override Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo) { if (!string.IsNullOrEmpty(context.ModelState.ValidationTargetPath)) { var validator = context.Services.GetRequiredService <IViewModelValidator>(); context.ModelState.Errors.AddRange(validator.ValidateViewModel(context.ModelState.ValidationTarget)); context.FailOnInvalidModelState(); } return(TaskUtils.GetCompletedTask()); }
/// <inheritdoc /> protected internal override Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception) => exception != null?OnCommandExceptionAsync(context, actionInfo, exception) : Task.FromResult(0);
/// <summary> /// Called after the command is executed. /// </summary> protected internal virtual void OnCommandExecuted(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception) { }
/// <summary> /// Resolves the command for the specified post data. /// </summary> public void ResolveCommand(DotvvmRequestContext context, DotvvmView view, string serializedPostData, out ActionInfo actionInfo) { // get properties var data = JObject.Parse(serializedPostData); var path = data["currentPath"].Values<string>().ToArray(); var command = data["command"].Value<string>(); var controlUniqueId = data["controlUniqueId"].Value<string>(); if (string.IsNullOrEmpty(command)) { // empty command actionInfo = null; } else { // find the command target if (!string.IsNullOrEmpty(controlUniqueId)) { var target = view.FindControl(controlUniqueId); if (target == null) { throw new Exception(string.Format("The control with ID '{0}' was not found!", controlUniqueId)); } actionInfo = commandResolver.GetFunction(target, view, context, path, command); } else { actionInfo = commandResolver.GetFunction(view, context, path, command); } } }
/// <summary> /// Called before the command is invoked. /// </summary> protected internal override void OnCommandExecuting(IDotvvmRequestContext context, ActionInfo actionInfo) { Authorize(context); base.OnCommandExecuting(context, actionInfo); }
/// <summary> /// Called before the command is executed. /// </summary> protected internal virtual void OnCommandExecuting(IDotvvmRequestContext context, ActionInfo actionInfo) { }
public void ProcessStaticCommandRequest(DotvvmRequestContext context) { JObject postData; using (var jsonReader = new JsonTextReader(new StreamReader(context.OwinContext.Request.Body))) { postData = JObject.Load(jsonReader); } // validate csrf token context.CsrfToken = postData["$csrfToken"].Value<string>(); CsrfProtector.VerifyToken(context, context.CsrfToken); var command = postData["command"].Value<string>(); var arguments = postData["args"].ToObject<object[]>(); var lastDot = command.LastIndexOf('.'); var typeName = command.Remove(lastDot); var methodName = command.Substring(lastDot + 1); var methodInfo = Type.GetType(typeName).GetMethod(methodName); if (!Attribute.IsDefined(methodInfo, typeof(StaticCommandCallableAttribute))) { throw new DotvvmHttpException("method validation failed"); } var actionInfo = new ActionInfo() { IsControlCommand = false, Action = () => methodInfo.Invoke(null, arguments) }; var filters = methodInfo.GetCustomAttributes<ActionFilterAttribute>(); foreach (var filter in filters) { filter.OnCommandExecuting(context, actionInfo); } Exception exception = null; object result = null; try { result = methodInfo.Invoke(null, arguments); } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ex.InnerException; } if (ex is DotvvmInterruptRequestExecutionException) { throw new DotvvmInterruptRequestExecutionException("The request execution was interrupted in the command!", ex); } exception = ex; } foreach (var filter in filters) { filter.OnCommandExecuted(context, actionInfo, exception); } if (exception != null) { throw new Exception("unhandled exception in command", exception); } if (result != null) { using (var writer = new StreamWriter(context.OwinContext.Response.Body)) { writer.WriteLine(JsonConvert.SerializeObject(result)); } } }
/// <summary> /// Called when the exception occurs during the command invocation. /// </summary> protected virtual void OnCommandException(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex) { }
/// <summary> /// Called after the command is invoked. /// </summary> protected internal override void OnCommandExecuted(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception) { if (exception != null) { OnException(context, actionInfo, exception); } }
protected Task ExecuteCommand(ActionInfo action, DotvvmRequestContext context, IEnumerable<ActionFilterAttribute> methodFilters) { // run OnCommandExecuting on action filters foreach (var filter in methodFilters) { filter.OnCommandExecuting(context, action); } object result = null; try { result = action.Action(); } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ex.InnerException; } if (ex is DotvvmInterruptRequestExecutionException) { throw new DotvvmInterruptRequestExecutionException("The request execution was interrupted in the command!", ex); } context.CommandException = ex; } // run OnCommandExecuted on action filters foreach (var filter in methodFilters.Reverse()) { filter.OnCommandExecuted(context, action, context.CommandException); } if (context.CommandException != null && !context.IsCommandExceptionHandled) { throw new Exception("Unhandled exception occured in the command!", context.CommandException); } return result as Task ?? (result == null ? TaskUtils.GetCompletedTask() : Task.FromResult(result)); }
/// <summary> /// Called when the exception occurs during the command invocation. /// </summary> protected virtual void OnException(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex) { }
/// <inheritdoc /> protected override Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo) => Authorize(context, null);
/// <inheritdoc /> protected override Task OnCommandExecutingAsync(IDotvvmRequestContext context, ActionInfo actionInfo) { Authorize(context, null); return(TaskUtils.GetCompletedTask()); }
protected internal override Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception) { if (exception != null) { return(OnCommandExceptionAsync(context, actionInfo, exception)); } else { return(Task.FromResult(0)); } }
/// <summary> /// Called before the command is invoked. /// </summary> protected internal override void OnCommandExecuting(DotvvmRequestContext context, ActionInfo actionInfo) { Authorize(context); base.OnCommandExecuting(context, actionInfo); }
protected internal override Task OnCommandExecutedAsync(IDotvvmRequestContext context, ActionInfo actionInfo, Exception?exception) { if (exception != null) { return(OnCommandExceptionAsync(context, actionInfo, exception)); } else { return(TaskUtils.GetCompletedTask()); } }