Informational (1xx)
100 Continue: The client should continue with the request.
Successful (2xx)
200 OK: The request has succeeded. This is typically used for a successful GET request or a successful PUT/POST request.
201 Created: The resource was successfully created. Typically used for POST requests when a new resource is created.
204 No Content: The request was successful, but there is no content to return. Commonly used for DELETE requests.
Redirection (3xx)
301 Moved Permanently: The requested resource has been permanently moved to a new URI.
302 Found: The requested resource has been temporarily moved.
Client Error (4xx)
400 Bad Request: The request is malformed or contains invalid data.
401 Unauthorized: The client is not authorized to access the resource.
403 Forbidden: The client does not have permission to access the resource.
404 Not Found: The requested resource could not be found.
409 Conflict: A conflict occurred while processing the request, such as trying to create a resource that already exists.
Server Error (5xx)
500 Internal Server Error: A generic error message indicating that something went wrong on the server.
502 Bad Gateway: The server received an invalid response from an upstream server.
503 Service Unavailable: The server is temporarily unavailable, often due to overload or maintenance.
Example of setting status codes in .NET Core Web API:
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IProductService _productService;
public ProductsController(IProductService productService)
{
_productService = productService;
}
[HttpGet("{id}")]
public IActionResult GetProduct(int id)
{
var product = _productService.GetProductById(id);
if (product == null)
{
return NotFound(); // 404 Not Found
}
return Ok(product); // 200 OK
}
[HttpPost]
public IActionResult CreateProduct(Product product)
{
if (product == null)
{
return BadRequest(); // 400 Bad Request
}
_productService.CreateProduct(product);
return CreatedAtAction(nameof(GetProduct), new { id = product.Id }, product); // 201 Created
}
[HttpPut("{id}")]
public IActionResult UpdateProduct(int id, Product product)
{
if (id != product.Id)
{
return BadRequest(); // 400 Bad Request
}
var existingProduct = _productService.GetProductById(id);
if (existingProduct == null)
{
return NotFound(); // 404 Not Found
}
_productService.UpdateProduct(product);
return NoContent(); // 204 No Content
}
[HttpDelete("{id}")]
public IActionResult DeleteProduct(int id)
{
var product = _productService.GetProductById(id);
if (product == null)
{
return NotFound(); // 404 Not Found
}
_productService.DeleteProduct(id);
return NoContent(); // 204 No Content
}
}
In this example:
NotFound() returns a 404 Not Found status code when the product is not found.
Ok() returns a 200 OK status code when the product is found.
BadRequest() returns a 400 Bad Request status code if the request is invalid.
CreatedAtAction() returns a 201 Created status code after successfully creating a new product.
NoContent() returns a 204 No Content status code after a successful delete or update where no additional data is returned.
These are the common patterns for status codes, but you can return any status code based on your application's needs using methods like StatusCode() if you need a custom code.
![.net core web api status code](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg4hYEZOKWhoKOaZyBxBsOwTJQ5LoZfQBfbYd1HcUTEmygp2axJnZ5SczML69JY5v8nvshXaZCr360PRvajG2PgLqZLxd2KFk72OvqJcft0fspciSUvD5zR5OHFX9WEyVTSyZdRNi2iSBgY8nQ-GE4V-bqrmeLjoy4DMSjBRkrirxhLQkJRYJ2s6ef1Tj_4/s72-c/asp.net%20core%20web%20api%20status%20code.png)
No comments: