Skip to content

HTTP Status Codes

Yann edited this page Aug 16, 2018 · 1 revision

What are HTTP status code

Http status code are standardized code to inform about the state of the request. They are splitted in 4 categories :

  1. Info (1xx)
  2. Success (2xx)
  3. Redirection (3xx)
  4. Client (4xx)
  5. Server (5xx)

See them all on httpstatuses.com

Using status codes in dotnet core 2

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);
        }
Clone this wiki locally