I am validating models in ASP.NET MVC, and throwing a custom Exception that contains a list of those errors. Is this a preferred, best practice, or should I return a strongly typed list of errors instead of using a throw new CustomException(List errors). I catch these errors regardless in my OnException in the BaseController to handle an ajax request or post back.
Asked
Active
Viewed 2,030 times
1 Answers
5
No. You should use ModelState
to store your validation errors. Exceptions should only be used in exceptional cases.
ModelState
has IsValid
and will return false
if there are any errors.

Daniel A. White
- 187,200
- 47
- 362
- 445
-
http://stackoverflow.com/questions/4776396/validation-how-to-inject-a-model-state-wrapper-with-ninject – Mike Flynn Mar 13 '12 at 17:16
-
Can we do this even in an API controller? – Worthy7 Oct 17 '16 at 02:58
-
@Worthy7 http://stackoverflow.com/questions/11686690/handle-modelstate-validation-in-asp-net-web-api – Daniel A. White Oct 17 '16 at 10:31