The HTTPstatus code when a resource is not found is 404 Not Found. This status code indicates that the server cannot find the requested resource. The resource may have been deleted, moved, or never existed in the first place.
To handle a 404 Not Found error, you can return a response with the following information:
The HTTP status code 404 Not Found.
A brief message indicating that the resource was not found.
A link to the home page or other relevant page.
The following code shows how to handle a 404 Not Found error in a .NET Core API:
C#
[ApiController]
[Route("[controller]")]
public class ProductController : ControllerBase {
[HttpGet("{id}")]
public IActionResult Get(int id) {
// Get the product data from the database.
// ...
// If the product data is not found, return a 404 Not Found response.
if (!productRepository.Exists(id)) {
return NotFound();
}
// Return the product data.
return Ok(product);
}
}
In this code, the Get() action method first tries to get the product data from the database. If the product data is not found, the method returns a 404 Not Found response.
Here are some additional tips for handling 404 Not Found errors:
Use a consistent error message across your API. This will help users understand what went wrong and how to fix it.
Use a descriptive error code. This will help users troubleshoot the error.
Provide a link to the home page or other relevant page. This will help users get back to where they were before the error occurred.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The HTTP status code when a resource is not found is 404 Not Found. This status code indicates that the server cannot find the requested resource. The resource may have been deleted, moved, or never existed in the first place.
To handle a 404 Not Found error, you can return a response with the following information:
The following code shows how to handle a 404 Not Found error in a .NET Core API:
C#
In this code, the
Get()action method first tries to get the product data from the database. If the product data is not found, the method returns a 404 Not Found response.Here are some additional tips for handling 404 Not Found errors: