Reputation: 11957
I've started decorating my IHttpActionResult
methods with a [ResponseType]
attribute, with the intent of making it easier for the consumer to know what to do with the response.
That makes sense for GET, because I'll probably want to do something with the returned data.
But does [ResponseType]
make any sense for PUT or POST requests, which don't return any data, just the success code?
e.g.
[HttpPut]
[Route("Contact/{contactId:int}/name", Name = "UpdateContactName")]
[ResponseType(typeof(?????))] // <- what should I put here? do I even need it at all?
public IHttpActionResult UpdateName(int contactId, [FromBody] string name)
{
//...
return StatusCode(HttpStatusCode.Accepted);
}
Upvotes: 8
Views: 6537
Reputation: 5306
I might be missing something but I personally find the following approach much more elegant and informative
[HttpPut]
public IHttpActionResult Approve(long id)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
// .....
// .....
bool success = false;// this is just a flag to indicate operation progress
// Do Update...
return Ok(sucess);
}
Rather than having a controller which doesn't return any data I would return 'something like' Ok(true) depending on the success or failure of the operation.
Upvotes: 1
Reputation: 11957
As it turns out, [ResponseType]
does make sense on controller methods that don't return data.
You can use a void
type to ensure that the WebApi helpfile does not display a "sample not available" message for these methods.
i.e.
[ResponseType(typeof(void))]
Upvotes: 12