Using Required and JsonRequired in ASP.NET Core Model Binding with JSON body

后端 未结 3 1901
北海茫月
北海茫月 2021-01-12 07:00

I\'m using ASP.NET Core 2.0, and I have a request object annotated like this:

public class MyRequest
{
    [Required]
    public Guid Id { get; set; }

    [         


        
3条回答
  •  一整个雨季
    2021-01-12 07:24

    When you use [FromBody] as binding source, the Model properties will get default values and [BindRequired] will be ignored. There is a related issue on "Problems with Parameter Validation".

    In this case is better to use [JsonRequired] instead of [BindRequired] to enforce binding properties.

    Note that [JsonRequired] affects serialization and deserialization both.

提交回复
热议问题