ASP.NET Core Web API Interview Question #17
When should a Web API return 204 No Content?
Web API & REST Fundamentals, Controllers, HTTP Methods & Status Codes Junior Beginner
Quick Interview Answer
204 No Content indicates that the request succeeded and there is intentionally no response body. It is commonly used after successful update or delete operations when the client does not need a representation returned.
Detailed Explanation
204 No Content indicates that the request succeeded and there is intentionally no response body. It is commonly used after successful update or delete operations when the client does not need a representation returned.
A 204 response must not contain a response body. If the API needs to return the updated resource or additional data, use an appropriate success status with content instead.
Code Example
[HttpDelete("{id:int}")]
public async Task<IActionResult> Delete(int id)
{
var deleted = await _service.DeleteAsync(id);
if (!deleted)
return NotFound();
return NoContent();
}