For a primer on HTTP status codes, check Frontend basics: Lecture 2
There are multiple possible status codes for each action
The ControllerBase the controllers should inherit from includes result methods for creating ActionResult objects
ControllerBase
ActionResult
IActionResult
200 - OK
Ok()
GET
DELETE
201 - Created
Created()
POST
204 - No content
NoContent()
PUT
PATCH
400 - Bad request
BadRequest()
404 - Not found
NotFound()
All actions
Custom
StatusCode()
[HttpGet("{id}")] public IActionResult GetContactById(int id) { // Contacts = list of contact objects, fetched from some repository var contact = Contacts.FirstOrDefault(c => c.Id == id); if (contact == null) { return NotFound(); } return Ok(contact); }
Contacts
Ok(contact)
400 (Bad Request)