How to put conditional Required Attribute into class property to work with WEB API?
问题 I just want to put conditional Required Attribute which is work with WEB API Example public sealed class EmployeeModel { [Required] public int CategoryId{ get; set; } public string Email{ get; set; } // If CategoryId == 1 then it is required } I am using Model State validation via ( ActionFilterAttribute ) 回答1: You can implement your own ValidationAttribute . Perhaps something like this: public class RequireWhenCategoryAttribute : ValidationAttribute { protected override ValidationResult