How to validate models when posting?

Topics: Web Api
Nov 2, 2011 at 9:11 PM

I have a set of services hosted with WCF Web Api, what I need to do is validate the properties inside the models of the app.

In MVC 3 for example I decorate properties in the model like this:

    [StringLength(30)]
   
public string UserName { get; set; }

and then in the controller I proceed like this to verify os the model has met the validation parameters:

    [HttpPost]
   
ActionResult Create(Model myModel)
   
{
       
if(ModelState.IsValid(){
           
Post the model
       
}
       
else
       
{
           
Don't post the model
       
}
   
}
Is there a way to do something similar in WCF Web Api?

Coordinator
Nov 2, 2011 at 10:55 PM

We don’t currently support model validation based on data annotations, but this is certainly something that we are looking at supporting in a future release.

Do you want to open an Issue Tracker item for this so that the community can vote on this feature?

You could also take a look at implementation validation yourself using the Validator APIs in System.ComponentModel.DataAnnotations.

Daniel Roth