API传参
在操作方法的参数前面使用Attribute
幂等性
对请求的数据验证
简单验证
usingSystem.ComponentModel.DataAnnotations;
publicclassTouristRouteForCreationDto
{
[Required(ErrorMessage="title不能为空")]
[MaxLength(1500)]
publicstringTitle { get; set; }
[Required]
[MaxLength(1500)]
publicstringDescription { get; set; }
}
属性级别验证
//增加验证Title和Description不能相同
usingSystem.ComponentModel.DataAnnotations;
publicclassTouristRouteForCreationDto:IValidatableObject
{
[Required(ErrorMessage="title不能为空")]
[MaxLength(1500)]
publicstringTitle { get; set; }
[Required]
[MaxLength(1500)]
publicstringDescription { get; set; }
publicIEnumerable<ValidationResult>Validate(ValidationContextvalidationContext)
{
if (Title==Description)
{
//yield return 保证ValidationResult后面的代码,下次调用还会继续执行
//TouristRouteForCreationDto代表错误路径
yieldreturnnewValidationResult("名称和描述不能相同", new[] { "TouristRouteForCreationDto"});
}
}
}
类级别验证
//创建Attribute
publicclassTouristRouteTitleMustBeDifferentFromDescriptionAttribute : ValidationAttribute
{
protectedoverrideValidationResult?IsValid(object?value, ValidationContextvalidationContext)
{
//获得实例
vartouristRouteDto= (TouristRouteForCreationDto)validationContext.ObjectInstance;
if (touristRouteDto.Title==touristRouteDto.Description)
{
returnnewValidationResult(
"路线名称必须与路线描述不同",
new[] { "TouristRouteForCreationDto" }
);
}
returnValidationResult.Success;
}
}
[TouristRouteTitleMustBeDifferentFromDescriptionAttribute]//应用属性
publicclassTouristRouteForCreationDto
{
[Required(ErrorMessage="title不能为空")]
[MaxLength(1500)]
publicstringTitle { get; set; }
[Required]
[MaxLength(1500)]
publicstringDescription { get; set; }
}
验证失败返回422状态码
//在program中设置
builder.Services.AddControllers(opt=> {
opt.ReturnHttpNotAcceptable=true;//可以接收其他格式如xml
}).AddXmlDataContractSerializerFormatters()
.ConfigureApiBehaviorOptions(setupAction=> {
setupAction.InvalidModelStateResponseFactory=context=>
{
varproblemDetail=newValidationProblemDetails(context.ModelState)
{
Type="无所谓",
Title="数据验证失败",
Status=StatusCodes.Status422UnprocessableEntity,
Detail="请看详细说明",
Instance=context.HttpContext.Request.Path//请求路径
};
problemDetail.Extensions.Add("traceId", context.HttpContext.TraceIdentifier);//追踪Id
returnnewUnprocessableEntityObjectResult(problemDetail)
{
ContentTypes= { "application/problem+json" }
};
};
});