Saturday, January 4, 2014

Aspect oriented model validations for WebAPI

We're building a brand new web app with the 'latest and greatest' stuff: MVC5, WebAPI 2 and AngularJS. I was looking for a nice way to do model validation in WebApi and I found this article:

http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api

Check it out - it's very elegant. So Angular's $http.post() will do some task and if things work out, you get the 400 statuscode and it'll call the success handler. If not, it'll trigger the error callback and I'm displaying the validation messages. Very clean, very re-usable and very few lines of code.

Works fine with a combination of attribute validations, like [Required] and [Range] with IValidateableObject.

No comments:

Post a Comment