Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通知用户基于多个属性进行自定义验证_C#_Validation_.net Core_Validationattribute - Fatal编程技术网

C# 如何通知用户基于多个属性进行自定义验证

C# 如何通知用户基于多个属性进行自定义验证,c#,validation,.net-core,validationattribute,C#,Validation,.net Core,Validationattribute,我正在尝试构建一个包含两个或多个属性的自定义消息验证 以下是我的DTO的简化版本: public class FooDTO { public int Id { get; set; } public int Name { get; set; } //more attributes... public bool IsValid { get {

我正在尝试构建一个包含两个或多个属性的自定义消息验证

以下是我的DTO的简化版本:

public class FooDTO
{
    public int Id { get; set; }
    public int Name { get; set; }
    //more attributes...

    public bool IsValid 
    {
        get
        {                                
            if (string.IsNullOrEmpty(this.Name) && (this.Id == 0))
                return false; //You will have to specify at least one of the following: Id or Name
            if (this.Name == "Boo" && this.ID = 999)
                return false;  //Boo name and Id of 99 are forbidden 
            //More validation ifs.

            return true;
        }
    }
}
我当前的控制器实现如下所示:

public async Task<IActionResult> DoSomething(FooDTO fooDTO)
{            
    if (!FooDTO.IsValid)
        return BadRequest(""); 
    // Rest of code        
}
公共异步任务DoSomething(FooDTO FooDTO)
{            
如果(!FooDTO.IsValid)
返回请求(“”);
//代码的其余部分
}
此实现不会使用相应的消息确认用户,例如当
Id
Name
都丢失时,我希望用户收到类似“您必须至少指定以下一项:Id或Name”验证错误的通知

有没有一种方法可以使用
ValidationAttribute
对另外两个涉及复杂验证的属性进行验证?
(这是我的解决方案)


或者用一种优雅的方法来构建要在
BadRequest(字符串消息)
重载?

Use中发送的自定义错误消息,可以使用
IValidatableObject
来实现自定义验证:

public class FooDTO : IValidatableObject
{
    public int Id { get; set; }

    public string Name { get; set; }
    //more attributes...

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (string.IsNullOrEmpty(Name) && (Id == 0))
            yield return new ValidationResult("You will have to specify at least one of the following: Id or Name", new[] { "Id", "Name" });

        if (Name == "Boo" && Id == 999)
            yield return new ValidationResult("Boo name and Id of 99 are forbidden", new[] { "Id", "Name" });
    }
}
公共类FooDTO:IValidatableObject
{
公共int Id{get;set;}
公共字符串名称{get;set;}
//更多属性。。。
公共IEnumerable验证(ValidationContext ValidationContext)
{
if(string.IsNullOrEmpty(Name)和&(Id==0))
yield return new ValidationResult(“您必须至少指定以下一项:Id或Name”,new[]{“Id”,“Name”}”);
如果(名称==“Boo”&&Id==999)
返回新的ValidationResult(“禁止Boo名称和99的Id”,新[]{“Id”,“name”});
}
}
在控制器中:

public async Task<IActionResult> DoSomething(FooDTO fooDTO)
{            
    if (!ModelState.IsValid)
        return BadRequest(ModelState); 

    // Rest of code        
}
公共异步任务DoSomething(FooDTO FooDTO)
{            
如果(!ModelState.IsValid)
返回请求(ModelState);
//代码的其余部分
}
欲了解更多信息,请阅读