Asp.net core IEnumerable参数URL中项数的基于属性的验证

Asp.net core IEnumerable参数URL中项数的基于属性的验证,asp.net-core,.net-core,Asp.net Core,.net Core,我正在寻找类似的内容,它为数据字段的值指定数值范围约束 我希望根据IEnumerable参数URL中的项数进行验证 比如说 [Route("foo")] [HttpGet] public ActionResult<SomeResponse>GetFoos([FromQuery] idList idList) { // Validate model and get stuff } public class IdList { // Obviously, the Range

我正在寻找类似的内容,它为数据字段的值指定数值范围约束

我希望根据IEnumerable参数URL中的项数进行验证

比如说

[Route("foo")]
[HttpGet]
public ActionResult<SomeResponse>GetFoos([FromQuery] idList idList)
{
   // Validate model and get stuff
}

public class IdList
{
    // Obviously, the Range attribute is wrong here
    [BindRequired, Range(1, 10, ErrorMessage = "Expected 1 to 10 ids.")]
    public List<string> Id { get; set; }
}
[路由(“foo”)]
[HttpGet]
public ActionResultGetFoos([FromQuery]idList idList)
{
//验证模型并获取内容
}
公营闲散者
{
//显然,Range属性在这里是错误的
[BindRequired,范围(1,10,ErrorMessage=“预期1到10个ID)。]
公共列表Id{get;set;}
}
使用.NET Core 3.1,您可以使用:

[必需,MinLength(2,ErrorMessage=“选择至少2项”)、MaxLength(4,ErrorMessage=“选择最多4项”)


或者您也可以自己创建。

完全可以触发上限。请注意,如果下限为1,则不会触发下限错误消息,因为Required已检查该消息。另一方面,您知道如何仅要求列表中的非空字符串吗?我目前没有任何想法,除了创建自己的属性,或者您可以使用另一个属性来检查该约束,如:
[Range(最小值:2,最大值:4,ErrorMessage=“预期2到4个非空ID”)]public int NonEmptyIds{get{return ids!=null?ids.Where(a=>!string.IsNullOrWhiteSpace(a)).Count():0;}