我需要 return 在 ASP.Net 核心 Web API 的验证属性中自定义验证结果(响应)

I need to return customized validation result (response) in validation attributes in ASP.Net core Web API

我需要 return 在 ASP.Net 核心 Web API 中自定义验证结果(响应)失效属性 这是我创建的 ValidationAttribute。

class MaxResultsAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        int maxResults = (int)value;

        if (maxResults <= 0)
        {
            return new CustomValidationResult(new ValidationResult("MaxResults should be greater than 0"));
        }

        return ValidationResult.Success;
    }
}

我创建了继承 ValidationResult 的 CustomValidationResult 对象,这样我就可以 return 我自己的自定义响应:

public class CustomValidationResult : ValidationResult
{
    public int FaultCode { get; set; }

    public string FaultMessage { get; set; }

    public CustomValidationResult(ValidationResult validationResult) : base(validationResult)
    {
        FaultCode = 123;
        FaultMessage = validationResult.ErrorMessage;
    }
}

但是没用 我需要 return 我自己的错误响应

实际响应:

    {
      "MaxResults": [
        "MaxResults should be greater than 0"
      ] 
    }

我期待的回应:

    {
      "code": "55",
      "message": "The following validation errors occurred: MaxResults should be greater than 0"
    }

The response I'm expecting:

{
  "code": "55",
  "message": "The following validation errors occurred: MaxResults should be greater than 0"
}

我已经使用你的代码重现了这个问题,使用 CustomValidationResult,我只能得到像你这样的实际响应。要实现上述行为,作为解决方法,我建议您可以尝试使用操作过滤器来处理验证失败错误响应。检查以下示例代码:

  1. 创建包含 returned 字段的自定义 ValidationError 模型:

     public class ValidationError
     {
         [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
         public string Field { get; }
    
         [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
         public int Code { get; set; }
    
         public string Message { get; }
    
         public ValidationError(string field,int code, string message)
         {
             Field = field != string.Empty ? field : null;
             Code = code != 0 ? code : 55;
             Message = message;
         }
     }
    
     public class ValidationResultModel
     {
         public string Message { get; }
    
         public List<ValidationError> Errors { get; }
    
         public ValidationResultModel(ModelStateDictionary modelState)
         {
             Message = "Validation Failed";
             Errors = modelState.Keys
                     .SelectMany(key => modelState[key].Errors.Select(x => new ValidationError(key,0, x.ErrorMessage)))
                     .ToList();
         }
     }
    
  2. 创建自定义 IActionResult。默认情况下,当显示验证错误时,它会 return BadRequestObjectResult 并且 HTTP 状态代码为 400。这里我们可以更改 Http 状态代码。

     public class ValidationFailedResult : ObjectResult
     {
         public ValidationFailedResult(ModelStateDictionary modelState)
             : base(new ValidationResultModel(modelState))
         {
             StatusCode = StatusCodes.Status422UnprocessableEntity; //change the http status code to 422.
         }
     }
    
  3. 创建自定义操作过滤器属性:

     public class ValidateModelAttribute: ActionFilterAttribute
     { 
         public override void OnActionExecuting(ActionExecutingContext context)
         {
             if (!context.ModelState.IsValid)
             {
                 context.Result = new ValidationFailedResult(context.ModelState);
             }
         }
     }
    
  4. 将Startup.ConfigureServices中的默认响应类型更改为SerializableError:

         services.AddControllers().ConfigureApiBehaviorOptions(options =>
         {
             options.InvalidModelStateResponseFactory = context =>
             {
                 var result = new ValidationFailedResult(context.ModelState);
    
                 // TODO: add `using System.Net.Mime;` to resolve MediaTypeNames
                 result.ContentTypes.Add(MediaTypeNames.Application.Json);
                 result.ContentTypes.Add(MediaTypeNames.Application.Xml);
    
                 return result;
             };
         });
    
  5. 在动作方法或控制器中添加自定义动作过滤器。

     [HttpPost]
     [ValidateModel]
     public async Task<ActionResult<Student>> PostStudent(Student student)
     { 
        ...
     }
    
  6. 创建具有自定义属性验证的 Student 模型(18 岁以上):

     public class Student
     {
         [Key]
         public int Id { get; set; }
    
         [Required(ErrorMessage = "Please enter name")]
         public string Name { get; set; }
    
         [Required(ErrorMessage = "Please choose admission date.")]
         [Display(Name = "Admission Date")]
         [DataType(DataType.Date)] 
         public DateTime AdmissionDate { get; set; }
    
         [Display(Name = "Date of Birth")]
         [DataType(DataType.Date)]
         [Min18Years]
         public DateTime DateofBirth { get; set; }
     }
    
    
     public class Min18Years : ValidationAttribute
     {
         protected override ValidationResult IsValid(object value, ValidationContext validationContext)
         {
             var student = (Student)validationContext.ObjectInstance;
    
             if (student.DateofBirth == null)
                 return new ValidationResult("Date of Birth is required.");
    
             var age = DateTime.Today.Year - student.DateofBirth.Year;
    
             if (age <= 0)
             { 
                 return new ValidationResult("MaxResults should be greater than 0");
             } 
             return (age >= 18)
                 ? ValidationResult.Success
                 : new ValidationResult("Student should be at least 18 years old.");
         }
     }  
    

应用运行后,结果是这样的:

参考:

Handle Validation failure errors in ASP.NET Core web APIs

Handling validation responses for ASP.NET Core Web API