Reputation: 24706
I need to implement validation in my web api controller. In my class I have a method like this:
public MyEntity Post(MyEntity entity)
{
// ...
}
In POST and PUT methods I usually return the created/updated object.
In this tutoral they are returning a HttpResponseMessage
so that they can do something like this:
if (ModelState.IsValid)
{
// Do something with the product (not shown).
return new HttpResponseMessage(HttpStatusCode.OK);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
Is there a way to use a similar approach returning the saved entity?
Upvotes: 2
Views: 232
Reputation: 1430
You should use HttpRequestMessageExtensions.CreateResponse method. For example:
if (ModelState.IsValid)
{
// Do something with the product (not shown).
return Request.CreateResponse<MyEntity>(HttpStatusCode.OK, entity);
}
Upvotes: 2