---
title: "How to handle model validation errors in a .NET Core API?"  
description: "How to handle model validation errors in a .NET Core API?"  
author: "Steilla Mitchel"  
published: 2023-08-28  
updated: 2023-08-29  
canonical: https://www.mindstick.com/forum/159706/how-to-handle-model-validation-errors-in-a-dot-net-core-api  
category: "asp.net core"  
tags: ["validation", "asp.net core", ".net core", ".net core api"]  
reading_time: 2 minutes  

---

# How to handle model validation errors in a .NET Core API?

How to [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) [model](https://yourviews.mindstick.com/view/81334/america-is-reopening-after-corona-lockdown-but-on-swedish-model) [validation errors](https://www.mindstick.com/forum/160109/how-to-handle-exceptions-caused-by-data-validation-errors-in-api-input-parameters) in a .NET [Core API](https://www.mindstick.com/forum/160547/how-to-pass-multiple-parameters-in-url-dot-net-core-api)?

## Replies

### Reply by Aryan Kumar

To handle [model validation](https://www.mindstick.com/blog/646/model-validation-in-asp-dot-net-mvc-4) [errors](https://answers.mindstick.com/qa/116170/fresh-fir-against-gandhis-in-national-herald-case-cover-up-for-ed-s-own-errors) in a .NET Core [API](https://www.mindstick.com/articles/12641/instagram-api-upgraded-to-facebook-graph), you can use the following steps:

1. Add validation attributes to your model properties. These attributes will be used to validate the data that is submitted to your API.
2. In your controller action, check the `ModelState` property. This property will contain a list of any validation errors that occurred.
3. If there are any validation errors, return a 400 Bad Request response. The response should include a JSON object that lists the validation errors.

The following code shows how to handle model validation errors in a .NET Core API:

C#

```plaintext
[ApiController]
[Route("[controller]")]
public class ProductController : ControllerBase {

  [HttpPost]
  public IActionResult Create([FromBody] Product product) {
    // Validate the product data.
    if (!ModelState.IsValid) {
      return BadRequest(ModelState.GetErrors());
    }

    // Save the product data.
    // ...

    return Ok(product);
  }
}
```

In this code, the `Create()` action method validates the product data before saving it to the database. If the data is invalid, the method returns a 400 Bad Request response.

Here are some additional things to keep in mind about handling model validation errors in a .NET Core API:

- You can use the `ModelState.IsValid` property to check if the model data is valid.
- You can use the `ModelState.GetErrors()` method to get a list of the validation errors.
- You can return a 400 Bad Request response to indicate that the request was invalid.
- You can include a JSON object in the response that lists the validation errors.


---

Original Source: https://www.mindstick.com/forum/159706/how-to-handle-model-validation-errors-in-a-dot-net-core-api

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
