I am developing a Restful service using .net web API.
There are a few posts about input validation for the post request using model validation. However, I am wondering what is the best practice of doing the validation for Get request.
For example
public HttpResponseMessage Get(int id, string type)
{
// validation
if (id <= 500 & id >= 0) {
// invalid request
throw new HttpResponseException();
}
// type validation
if (type is not in a predefined allowed type list from database) {
// throw validation error
}
// more validation ... ...
// do something else
}
I would like to know what is the best place to put the validation logic in in .net web api framework.
The id validation is just an example and the validation logic could go quite complicated for some cases.
I don't want to create a class just for the id and put some custom validator attribute on the ID property. I think .net has a better support for that already.
id 122334534
and there is no record in the database for thatid
, you would just return404 Not Found
anyway... – Trevor Pilley Jun 10 '13 at 13:19