1
I am doing an API in . NET Core and I have a question in what I should return in the methods Httppost and Httput, as below:
// POST: api/Product
[HttpPost]
public async Task<IActionResult> Post([FromBody] ProductDto product)
{
try
{
await _productAppService.Add(product);
return Ok();
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex);
}
}
// PUT: api/Product/5
[HttpPut("{id}")]
public async Task<IActionResult> Put(Guid id, [FromBody] ProductDto product)
{
try
{
var success = await _productAppService.Update(id, product);
return success ? Ok() : (IActionResult)NotFound();
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex);
}
}
As you can see, when making a POST I simply return an OK and when making a PUT, if it finds the ID of a product, it returns OK, otherwise it returns Notfound.
Aiming at best practices, what would be necessary to return in these methods? Like a success flag and a newly stored model, or a simple OK is enough? or does it depend on the context in which we are working?
Take a look at this link: https://docs.microsoft.com/pt-br/azure/architecture/best-practices/api-design
– Bruno Warmling