any one tell that how to validate in Model in web api core using data annotation?
Loading
any one tell that how to validate in Model in web api core using data annotation?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sandhiya PriyaPosted Nov 20, 2025, 4:04 AM
1. Add Data Annotation Attributes in Your Model
ASP.NET Core automatically validates these before entering your controller action.
Example Model
2. Validate in Controller Using ModelState
ASP.NET Core automatically populates ModelState.
3. Enable Automatic Validation (Built-in in ASP.NET Core 3+)
From .NET Core 3 and above, automatic model validation is enabled by default.
Meaning:
1. You don't need to manually check
ModelState.IsValid2. The framework returns 400 BadRequest with validation errors automatically.
But if you prefer manual checking (recommended for custom responses), use the previous method.
4. Optional: Customize Validation Response
If you want cleaner error messages:
Bonus: Custom Validation Attribute Example
Use it like:
Summary