-
Notifications
You must be signed in to change notification settings - Fork 5
HTTP Status Codes
Yann edited this page Aug 16, 2018
·
1 revision
Http status code are standardized code to inform about the state of the request. They are splitted in 4 categories :
- Info (1xx)
- Success (2xx)
- Redirection (3xx)
- Client (4xx)
- Server (5xx)
See them all on httpstatuses.com
In your API, all your controller have to inherit from the Controller class that also inherit from the abstract ControllerBase class. (see aspnet core source file ControllerBase.cs )
The ControllerBase already contains all you need to return automatically the status codes from the given methods like Ok(...), BadRequest(...), NotFound(...), etc.
ex:
[HttpGet]
[Route("{id}")]
public IActionResult GetUser(Guid id)
{
if (id == Guid.Empty)
{
return this.BadRequest();
}
if (!this.UsersServices.UserExists(id))
{
return this.NotFound($"User with id {id} not found");
}
var user = this.UsersServices.GetUserById(id);
var userDto = Mapper.Map<UserDto>(user);
return this.Ok(userDto);
}