C# 自定义验证的ValidationAttribute无法检查null或空(服务器端)
我发现,当attrib不是调用查询的一部分,而是方法签名的一部分时,不会调用自定义ValidationAttribute的IsValid()方法 所以,我想知道RequiredAttribute如何适用于QueryString的某些参数。我看了一下RequiredAttribute源代码,它会遇到与我将描述的相同的问题 例如,使用以下操作方法签名:C# 自定义验证的ValidationAttribute无法检查null或空(服务器端),c#,asp.net-core,asp.net-web-api,C#,Asp.net Core,Asp.net Web Api,我发现,当attrib不是调用查询的一部分,而是方法签名的一部分时,不会调用自定义ValidationAttribute的IsValid()方法 所以,我想知道RequiredAttribute如何适用于QueryString的某些参数。我看了一下RequiredAttribute源代码,它会遇到与我将描述的相同的问题 例如,使用以下操作方法签名: public ActionResult<IEnumerable<SpaceBody>> Get( [
public ActionResult<IEnumerable<SpaceBody>> Get(
[FromQuery]
[SomeAttribute]string some,
int minDistance, int maxDistance)
它包含拒绝空值的逻辑(正如RequiredAttribute所做的那样),因此如果我使用[Required,Some],它将两次显示任何null或空参数调用,这是不可分配的
如果我只保留[Some],将跳过null或空错误,这是可能发生的最糟糕的事情
我现在看到的唯一解决方案是从定制attrib中删除null或空逻辑,并使用[Required,Some]。好的,这可能是一个解决方案,但因为动机是错误的:“因为我不能在null或空值的情况下激发自定义属性”。这是设计的吗?在这种情况下,RequiredAttribute工作并使其他属性被激发的魔力是什么
要完成上下文,这是我的Startup.ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore(o =>
{
//o.Filters.Add<ValidateModelAttribute>();
o.EnableEndpointRouting = false;
})
.AddApiExplorer()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonFormatters()
.AddDataAnnotations()
.AddControllersAsServices();
}
抱歉提供了太多的详细信息,最后我只想知道如何仅使用[Some]来执行所有与“Some”相关的验证?而是从
RequiredAttribute
继承,因为这继承自ValidationAttribute
public class SomeAttribute :RequiredAttribute
{
private const string SOME_REGEX = @"(?i)^\b(xx)\b$";
private const string SOME_NOT_VALID_ERROR_MESSAGE = "Some is not valid.";
private const string SOME_REQUIRED_ERROR_MESSAGE = "Some must have a value.";
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var some = Convert.ToString(value);
if (string.IsNullOrWhiteSpace(some))
return new ValidationResult(SOME_REQUIRED_ERROR_MESSAGE);
if (!Regex.Match(some, SOME_REGEX, RegexOptions.IgnoreCase).Success)
return new ValidationResult(SOME_NOT_VALID_ERROR_MESSAGE);
return ValidationResult.Success;
}
}
感谢您的回答,这只是POC,但在实际场景中,我们需要从[Company]ValidationAttribute继承,它不能是RequiredAttribute的后代。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore(o =>
{
//o.Filters.Add<ValidateModelAttribute>();
o.EnableEndpointRouting = false;
})
.AddApiExplorer()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonFormatters()
.AddDataAnnotations()
.AddControllersAsServices();
}
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
context.Result = new BadRequestObjectResult(context.ModelState);
}
}
}
public class SomeAttribute :RequiredAttribute
{
private const string SOME_REGEX = @"(?i)^\b(xx)\b$";
private const string SOME_NOT_VALID_ERROR_MESSAGE = "Some is not valid.";
private const string SOME_REQUIRED_ERROR_MESSAGE = "Some must have a value.";
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var some = Convert.ToString(value);
if (string.IsNullOrWhiteSpace(some))
return new ValidationResult(SOME_REQUIRED_ERROR_MESSAGE);
if (!Regex.Match(some, SOME_REGEX, RegexOptions.IgnoreCase).Success)
return new ValidationResult(SOME_NOT_VALID_ERROR_MESSAGE);
return ValidationResult.Success;
}
}