Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which ResponseType should be used for the IHttpActionResult of a PUT or POST request?

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);
}
like image 700
demoncodemonkey Avatar asked Mar 26 '14 16:03

demoncodemonkey


People also ask

When should I use IHttpActionResult?

If you want to return a response which needs to be constructed through a complex logic, say lots of response headers, etc, you can abstract all those logic into an action result class implementing IHttpActionResult and use it in multiple action methods to return response.

How do I return IHttpActionResult in Web API?

IHttpActionResult contains a single method, ExecuteAsync, which asynchronously creates an HttpResponseMessage instance. If a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync method to create an HttpResponseMessage. Then it converts the HttpResponseMessage into an HTTP response message.

What is the namespace for IHttpActionResult return type in Web API?

Essentially, IHttpActionResult is a factory for HttpResponsemessage. The IHttpActionResult interface is contained in the System. Web. Http namespace and creates an instance of HttpResponseMessage asynchronously.

What is the difference between IActionResult and IHttpActionResult?

What is the difference between IHttpActionResult and IActionresult ? "IActionResult is the new abstraction that should be used in your actions. Since Web API and MVC frameworks have been unified in ASP.NET Core, various IActionResult implementations can handle both traditional API scenarios.".


2 Answers

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))]
like image 111
demoncodemonkey Avatar answered Oct 06 '22 01:10

demoncodemonkey


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.

like image 26
MHOOS Avatar answered Oct 06 '22 00:10

MHOOS