https://github.com/fkucukkara/errorresponseworkshop
This repository demonstrates an implementation of **error response handling** in .NET Minimal APIs using `ProblemDetails`.
https://github.com/fkucukkara/errorresponseworkshop
csharp error-handling netcore-webapi problem-details
Last synced: 17 days ago
JSON representation
This repository demonstrates an implementation of **error response handling** in .NET Minimal APIs using `ProblemDetails`.
- Host: GitHub
- URL: https://github.com/fkucukkara/errorresponseworkshop
- Owner: fkucukkara
- License: mit
- Created: 2024-12-28T20:54:52.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2025-02-10T19:43:42.000Z (3 months ago)
- Last Synced: 2025-03-29T03:11:36.392Z (about 1 month ago)
- Topics: csharp, error-handling, netcore-webapi, problem-details
- Language: C#
- Homepage:
- Size: 6.84 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Error Response Handling in Minimal APIs
This repository demonstrates an implementation of **error response handling** in .NET Minimal APIs using `ProblemDetails`. It showcases how to handle exceptions and status codes in a structured and user-friendly way while adhering to best practices.
## Features
- **Standardized Error Responses**: Uses `ProblemDetails` to generate error responses conforming to the [RFC 7807](https://datatracker.ietf.org/doc/html/rfc7807) standard.
- **Automatic Error Handling**: Simplifies the handling of exceptions and HTTP status codes with middleware integration.
- **Minimal API Integration**: Lightweight and concise implementation for modern .NET applications.## Advantages
1. **Improved API Consumer Experience**:
- Provides consistent and meaningful error messages to API clients.
- Helps consumers of the API understand what went wrong and how to resolve issues.2. **Standardized Format**:
- The use of `ProblemDetails` ensures that error responses follow a predictable structure, which is crucial for debugging and integration with client applications.3. **Reduced Boilerplate**:
- The integration of `ProblemDetails`, exception handling, and status code pages reduces the need for custom error-handling code.4. **Extensibility**:
- Easily customizable to include additional error details, such as trace IDs, request URLs, or custom metadata.## Implementation
The implementation uses Minimal APIs and middleware to handle exceptions and status codes.### Code Example
```csharp
var builder = WebApplication.CreateBuilder(args);builder.Services.AddProblemDetails();
var app = builder.Build();
app.UseExceptionHandler();
app.UseStatusCodePages();app.MapGet("/users/{id:int}", (int id)
=> id <= 0 ? Results.BadRequest() : Results.Ok(new User(id)));app.Run();
internal record User(int Id);
```### Explanation
1. **`AddProblemDetails`**:
- Registers the `ProblemDetails` middleware, which generates standardized error responses.2. **`UseExceptionHandler`**:
- Handles unhandled exceptions and generates error responses.3. **`UseStatusCodePages`**:
- Adds support for returning proper responses for HTTP status codes (e.g., `400 Bad Request`, `404 Not Found`).4. **Endpoint**:
- Example endpoint `/users/{id:int}`:
- Returns `400 Bad Request` if `id <= 0`.
- Returns a `200 OK` response with a user object if `id > 0`.## How to Run
1. Clone the repository:
```bash
git clone https://github.com/fkucukkara/errorResponseWorkshop.git
cd src/API
```2. Build and run the application:
```bash
dotnet run
```3. Test the endpoints:
- Valid request:
```
GET /users/1
Response: { "id": 1 }
```
- Invalid request:
```
GET /users/-1
Response: HTTP 400 with ProblemDetails
```## Example Error Response
For an invalid request, the API generates a response in the `ProblemDetails` format:```json
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "Bad Request",
"status": 400,
"detail": "The request parameters are invalid.",
"instance": "/users/-1"
}
```## License
[](LICENSE)This project is licensed under the MIT License, which allows you to freely use, modify, and distribute the code. See the [`LICENSE`](LICENSE) file for full details.