Which ResponseType should I use for an IHttpActionResult of a PUT or POST request?

I began to decorate IHttpActionResult methods with the [ResponseType] attribute in order to make it easier for the consumer to know what to do with the response.

This makes sense for GET because I probably want to do something with the returned data.
But does [ResponseType] make sense for PUT or POST requests that do not return any data, but only a success code?

eg.

 [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); } 
+6
source share
2 answers

As it turns out, [ResponseType] makes sense for controller methods that don't return data.

You can use the void type to ensure that the WebApi help file does not display a “sample not available” message for these methods.

i.e.

 [ResponseType(typeof(void))] 
+8
source

I might be missing something, but I personally find the following approach 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); } 

Instead of having a controller that returns no data, I would return “something like” “OK” (true) depending on the success or failure of the operation.

+1
source

All Articles