Example #1
0
        /// <summary>
        /// Produces a <see cref="StatusCodes.Status400BadRequest"/> response
        /// with a <see cref="HttpValidationProblemDetails"/> value.
        /// </summary>
        /// <param name="errors">One or more validation errors.</param>
        /// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
        /// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
        /// <param name="statusCode">The status code.</param>
        /// <param name="title">The value for <see cref="ProblemDetails.Title" />. Defaults to "One or more validation errors occurred."</param>
        /// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
        /// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
        /// <returns>The created <see cref="IResult"/> for the response.</returns>
        public static IResult ValidationProblem(
            IDictionary <string, string[]> errors,
            string?detail   = null,
            string?instance = null,
            int?statusCode  = null,
            string?title    = null,
            string?type     = null,
            IDictionary <string, object?>?extensions = null)
        {
            var problemDetails = new HttpValidationProblemDetails(errors)
            {
                Detail   = detail,
                Instance = instance,
                Type     = type,
                Status   = statusCode,
            };

            problemDetails.Title = title ?? problemDetails.Title;

            if (extensions is not null)
            {
                foreach (var extension in extensions)
                {
                    problemDetails.Extensions.Add(extension);
                }
            }

            return(new ObjectResult(problemDetails)
            {
                ContentType = "application/problem+json",
            });
        }
Example #2
0
        /// <summary>
        /// Produces a <see cref="StatusCodes.Status400BadRequest"/> response
        /// with a <see cref="HttpValidationProblemDetails"/> value.
        /// </summary>
        /// <param name="errors">One or more validation errors.</param>
        /// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
        /// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
        /// <param name="statusCode">The status code.</param>
        /// <param name="title">The value for <see cref="ProblemDetails.Title" />.</param>
        /// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
        /// <returns>The created <see cref="IResult"/> for the response.</returns>
        public static IResult ValidationProblem(
            IDictionary <string, string[]> errors,
            string?detail   = null,
            string?instance = null,
            int?statusCode  = null,
            string?title    = null,
            string?type     = null)
        {
            var problemDetails = new HttpValidationProblemDetails(errors)
            {
                Detail   = detail,
                Instance = instance,
                Title    = title,
                Type     = type,
                Status   = statusCode,
            };

            return(new ObjectResult(problemDetails)
            {
                ContentType = "application/problem+json",
            });
        }