asp.net-coreasp.net-core-mvc.net-5model-validationvalidationattribute

In net 5, when binding from body, how to specify that all properties must be present?


Attributes like BindRequired make an action parameter to be required, even the usual model validation attributes can be applied. How can you make all properties from a class be required to be present in the request body?


Solution

  • If you have access to change the class: decorate all the properties with[Required] or [BindRequired].

    If you don't, you should make a DTO (Data Transfer Object) for that class and apply [Required] or [BindRequired] attributes.