Mike Flynn
Mike Flynn

Reputation: 24325

Custom Validation By Throwing Exceptions in ASP.NET MVC

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.

Upvotes: 1

Views: 2079

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 191048

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.

Upvotes: 5

Related Questions