Required vs BindRequired

  • Reading time:2 mins read

How do you ensure that the API receives the correct model from the user? DataAnnotation comes to the rescue here. Which attribute should you use? Required vs BindRequired? In this post we will focus on [RequiredAttribute] and [BindRequiredAttribute] differences.

They look similar, but their behavior is different. Both attributes ensure that a value of a property has been provided. [BindRequired] has been introduced with .NET Core. You can still use [Required]. Let’s have a look at example.

public class Product
{
    [Required]
    public string Name { get; set; }

    [Required]
    public decimal? Price { get; set; }
}

Please note that a Price is nullable. In “classic” MVC this is the way to achieve correct ModelState validation. Now we validate a ModelState.

public IActionResult Create([FromQuery]Product product) 
{ 
    if (!ModelState.IsValid) 
    { 
        return BadRequest(ModelState); 
    } 

    return Ok(product);
}

Let’s make API request with missing properties. In this case the response will be a 400 status.
“Ok, but I don’t want Price nullable.”
It’s additional effort to operate on nullable type. What happens if we get rid of nullable? Let’s take a look.
First modify the model.

public class Product 
{ 
    [Required]
    public string Name { get; set; }

    [Required]
    public decimal Price { get; set; }
}

Now request. In this case we’ll send Name and null for Price. The response will be a 200 status. But why? We set Price as required field. The default value for a decimal is 0. In this case the attribute has no effect. Price will be 0 even if user pass null. ModelState is valid.
[BindRequired] comes to action!
First modify the model.

public class Product 
{ 
    [Required] 
    public string Name { get; set; } 

    [BindRequired] 
    public decimal Price { get; set; } 
}

Let’s make the same request as above. The result will be a 400 status.

BindRequired works the same as Required. Additionally it checks that the value comes from the request. In this case null has been rejected.

References:

Want to read more? Leave a comment!

 

Subscribe
Notify of

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments