Esempio n. 1
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (!context.ModelState.IsValid)
            {
                var errorsInModelState = context.ModelState
                                         .Where(x => x.Value.Errors.Count > 0)
                                         .ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Errors.Select(x => x.ErrorMessage));

                //var errorResponse = new ErrorResponse();
                var errorResponse = new BaseResponse <object>();

                // Populate the errors in error response
                foreach (var error in errorsInModelState)
                {
                    errorResponse.AddErrorResponse(error.Key, error.Value.ToArray());
                }

                // return the error to the end users.
                context.Result = new BadRequestObjectResult(errorResponse);
                return;
            }

            await next();

            // after controller
            //...
        }