Skip to main content

That One line you missed that’s causing weird ASP.NET Core validation Issues!

· 3 min read
Rajiv Karthik Yanamandra
Senior Software Engineer @ Compare The Market, Australia

Forgot this line? Your ASP.NET Core app validation might act strange!

Okay, confession time—I recently encountered a strange issue with my ASP.NET Core validation in an inherited codebase. After some digging, I realized that a simple but crucial step had been missed, causing some pretty frustrating errors. I wanted to share this information with you, and perhaps with my future self, to ensure that we never encounter this issue again.

thumbnail post

Stick with me for the next few minutes, and you’ll learn how to take full control of your model validation in ASP.NET Core, avoiding those annoying, unexpected validation errors that can really throw a wrench in your project.

So here's what happened. ASP.NET Core is really into using data annotations for validation. It's like a kid with a new toy. However, when using FluentValidation, you don't need or want those built-in validators to interfere with your setup. They can cause redundant checks or conflicts, leading to some pretty strange behaviour in your app.

While working on that bug in this inherited codebase the other day, I found that the original setup didn’t account for clearing out those default model validators, which is crucial to ensuring FluentValidation works without interference. Before fixing this, whenever a validation error occurred—like passing a null value for the username—the response would look something like this:

{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"UserName": [
"The UserName field is required."
]
},
"traceId": "00-54173eba85492e9c17e12babf8c19ed3-f97dca20a4c12b47-00"
}

This error response was due to the default data annotation validators kicking in. It never even hit the controller during debugging, which made diagnosing the issue a bit tricky.

The fix is super simple, though. You just need to add this little line to your service configuration:

Here’s the code you need:

services.AddControllers(options =>
{
options.ModelValidatorProviders.Clear(); // Removes all data annotation validation providers
})
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());

This line basically says, “Hey, ASP.NET Core, thanks for the help, but we’ve got this covered.” It clears out all the data annotation validators, letting FluentValidation handle everything just the way you set it up. After adding this line, the validation errors became much more manageable and looked like this from the FluentValidation library:

{
"type": "Validation",
"title": "ValidationError",
"status": 400,
"detail": "A validation problem occurred.",
"Extension": [
{
"code": "request.UserName",
"message": "UserName field is required."
}
]
}

No more surprises, just smooth sailing.

Conclusion

So, for future me and everyone else, don't skip this step! Adding that line will make your validation life a whole lot easier. Let’s keep things clean and simple together!

As always, here’s to a productive day with warm chai ☕, crunchy biscuits 🍪, and bug-free code 🐞.

🎉Happy coding!!🎉

🔥 Keep the Chai Brewing!

If this post added value to your day,consider adding value to mine with a coffee ☕ (or chai 🍵!). Every sip counts—thank you for your support!