
January 28, 2026 06:38 by
Peter
Correctly managing errors is just as crucial when creating APIs in ASP.NET Core as managing successful answers. It is challenging for client apps to comprehend and handle the plain text error messages that many applications return.
Problem Details, a common and organized method of returning error information, is how ASP.NET Core tackles this issue.
Problem Details: What Are They?
RFC 7807 defines the standard error response format known as Problem Details.It offers a standard JSON structure for errors, such as:
- HTTP status code
- A brief title for the error
- Detailed explanation is optional.
Problem Details are built right into ASP.NET Core.
Why Do Problem Details Matter?
Take a look at this error response:
Consider this error response:
"User not found"
This response:
- Is not structured
- Is hard to parse in frontend applications
- Provides very little context
With Problem Details, error responses become clear, consistent, and easy to handle.
Simple 404 Example Using Built-in Support
For example, an API attempts to fetch a user by ID, but the user does not exist.
Controller Code :
[HttpGet("users/{id}")]
public IActionResult GetUser(int id)
{
return NotFound();
}
Response Returned by ASP.NET Core :
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.5",
"title": "Not Found",
"status": 404
}
This approach requires no extra code, uses a standard error format, and is ideal for simple use cases
Complete 404 Example with Custom Problem Details
Sometimes, we need to give more meaningful information to the client, such as which resource was not found.
Step 1: User Model
public class UserDto
{
public int Id { get; set; }
public string Email { get; set; }
}
Step 2: Users Controller
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private static readonly List<UserDto> Users = new()
{
new UserDto { Id = 1, Email = "[email protected]" }
};
[HttpGet("{id}")]
public IActionResult GetUser(int id)
{
var user = Users.FirstOrDefault(u => u.Id == id);
if (user == null)
{
return NotFound(new ProblemDetails
{
Title = "User not found",
Detail = $"User with id {id} does not exist",
Status = StatusCodes.Status404NotFound,
Type = "https://httpstatuses.com/404"
});
}
return Ok(user);
}
}
Step 3: API Request
GET /api/users/10
Step 4: 404 Problem Details Response
{
"type": "https://httpstatuses.com/404",
"title": "User not found",
"status": 404,
"detail": "User with id 10 does not exist"
}
This response clearly explains what went wrong, why it occurred, and which resource caused the issue. Creating aProblemDetails object manually every time is unnecessary because ASP.NET Core automatically generates them for common errors, such as 404 (Not Found) and 400 (Bad Request). You should only create a custom ProblemDetails object when you need to provide additional clarity or specific metadata for an issue.
Conclusion
In this article we have seen, how Problem Details provides a clean, standard, and beginner-friendly way to handle errors in ASP.NET Core APIs.
Developers can rely on the framework’s default behavior and customize error responses only when needed. Hope you enjoy reading this.