1

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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Can we do this even in an API controller?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.