/// <summary>
        /// Apply the given dev details if its enabled.
        /// </summary>
        public static void ApplyDeveloperDetailsIfEnabled(ModelValidatedResult result, string devDetails)
        {
            if (string.IsNullOrWhiteSpace(devDetails) || EnableDeveloperDetails?.Invoke() != true)
            {
                return;
            }

            DeveloperDetailsAction?.Invoke(result, devDetails);
        }
示例#2
0
        /// <summary>
        /// Create a json result without any errors.
        /// </summary>
        public static ActionResult CreateJsonResultSuccess(this ModelStateDictionary modelState,
                                                           IEnumerable <string> flags          = null,
                                                           JsonRequestBehavior requestBehavior = JsonRequestBehavior.AllowGet,
                                                           int?statusCode    = null,
                                                           string devDetails = null)
        {
            var resultObject = new ModelValidatedResult
            {
                Success = true,
                Flags   = flags.CreateHashSet()
            };

            QoDLDataAnnotationsGlobalConfig.PostProcessResult(resultObject, devDetails);
            return(new QoDLJsonContentResult(resultObject, requestBehavior, statusCode));
        }
        private static void DefaultDeveloperDetailsAction(ModelValidatedResult result, string devDetails)
        {
            if (string.IsNullOrWhiteSpace(devDetails) || result == null)
            {
                return;
            }

            if (result.Success)
            {
                result.Flags.Add($"Developer details: {devDetails}");
            }
            else
            {
                result.Error += $"\n\n Developer details: {devDetails}";
            }
        }
示例#4
0
        /// <summary>
        /// Create a json result without any errors.
        /// </summary>
        public static IActionResult CreateJsonResultSuccess <TData>(this ModelStateDictionary modelState,
                                                                    TData data,
                                                                    IEnumerable <string> flags = null,
                                                                    bool allowGet     = true,
                                                                    int?statusCode    = null,
                                                                    string devDetails = null)
        {
            var resultObject = new ModelValidatedResult <TData>
            {
                Success = true,
                Data    = data,
                Flags   = flags.CreateHashSet()
            };

            QoDLDataAnnotationsGlobalConfig.PostProcessResult(resultObject, devDetails);
            return(new QoDLJsonContentResult(resultObject, allowGet, statusCode));
        }
示例#5
0
        /// <summary>
        /// Create a json result with a dictionary containing any invalid property names and their errors.
        /// </summary>
        public static ActionResult CreateJsonResultWithError(this ModelStateDictionary modelState,
                                                             string error,
                                                             IEnumerable <string> flags          = null,
                                                             JsonRequestBehavior requestBehavior = JsonRequestBehavior.AllowGet,
                                                             int?statusCode    = null,
                                                             string devDetails = null)
        {
            var modelErrors = modelState.GetModelErrorsAsDictionary();

            var resultObject = new ModelValidatedResult
            {
                Success     = modelState.IsValid && string.IsNullOrWhiteSpace(error),
                Error       = error,
                ModelErrors = modelErrors,
                Flags       = flags.CreateHashSet()
            };

            QoDLDataAnnotationsGlobalConfig.PostProcessResult(resultObject, devDetails);
            return(new QoDLJsonContentResult(resultObject, requestBehavior, statusCode));
        }
示例#6
0
        /// <summary>
        /// Create a json result with a dictionary containing any invalid property names and their errors.
        /// <para>Optionally with an error message if any model state issues were found.</para>
        /// </summary>
        public static IActionResult CreateJsonResult(this ModelStateDictionary modelState,
                                                     IEnumerable <string> flags = null,
                                                     bool allowGet  = true,
                                                     int?statusCode = null,
                                                     string errorMessageIfNotSuccess = null,
                                                     string devDetails = null)
        {
            var modelErrors = modelState.GetModelErrorsAsDictionary();

            var success      = modelState.IsValid;
            var resultObject = new ModelValidatedResult
            {
                Success     = success,
                Error       = success ? null : errorMessageIfNotSuccess,
                ModelErrors = modelErrors,
                Flags       = flags.CreateHashSet()
            };

            QoDLDataAnnotationsGlobalConfig.PostProcessResult(resultObject, devDetails);
            return(new QoDLJsonContentResult(resultObject, allowGet, statusCode));
        }
示例#7
0
        /// <summary>
        /// Create a json result with a dictionary containing any invalid property names and their errors.
        /// </summary>
        public static IActionResult CreateJsonResult <TData>(this ModelStateDictionary modelState,
                                                             TData data,
                                                             IEnumerable <string> flags = null,
                                                             string error      = null,
                                                             bool allowGet     = true,
                                                             int?statusCode    = null,
                                                             string devDetails = null)
        {
            var modelErrors = modelState.GetModelErrorsAsDictionary();

            var resultObject = new ModelValidatedResult <TData>
            {
                Success     = modelState.IsValid && string.IsNullOrWhiteSpace(error),
                Data        = data,
                Flags       = flags.CreateHashSet(),
                Error       = error,
                ModelErrors = modelErrors
            };

            QoDLDataAnnotationsGlobalConfig.PostProcessResult(resultObject, devDetails);
            return(new QoDLJsonContentResult(resultObject, allowGet, statusCode));
        }
 /// <summary>
 /// This method is invoked after result model is created by any extension method.
 /// </summary>
 public static ModelValidatedResult PostProcessResult(ModelValidatedResult result, string devDetails)
 {
     ApplyDeveloperDetailsIfEnabled(result, devDetails);
     return(ResultPostProcessAction?.Invoke(result, devDetails) ?? result);
 }