RA.Utilities.Api
· 2 min read
Version 10.0.0-rc.2
This release modernizes the RA.Utilities.Api package, introducing a suite of tools to build robust, consistent, and maintainable ASP.NET Core APIs. Key features include a .NET 8 global exception handler, helpers for standardized success responses, and a clean pattern for endpoint registration.
✨ New Features & Improvements
-
Global Exception Handling (
AddRaExceptionHandling):- Introduced a .NET 8
IExceptionHandlerimplementation that automatically catches exceptions and transforms them into standardized JSON error responses. - Catches semantic exceptions from
RA.Utilities.Core.Exceptions(e.g.,NotFoundException,ConflictException) and maps them to the correct HTTP status codes (404, 409, etc.). - Handles any unhandled exceptions by returning a generic 500 Internal Server Error to prevent leaking sensitive information.
- Introduced a .NET 8
-
Endpoint Registration Helpers (
AddEndpoints&MapEndpoints):- Provides a clean pattern for organizing API endpoints into separate files using the
IEndpointinterface. - Keeps
Program.csclean and maintainable by automatically discovering and registering all endpoint implementations in your project.
- Provides a clean pattern for organizing API endpoints into separate files using the
-
Standardized Success Response Helpers (
SuccessResponse):- Added a new static
SuccessResponseclass with helper methods (Ok,Created,NoContent, etc.). - These helpers simplify the creation of successful API responses and automatically wrap the payload in the standard
SuccessResponse<T>model, ensuring consistency with error responses.
- Added a new static
-
Seamless
Result<T>Integration:- The
SuccessResponsehelpers and the exception handling middleware work together to provide a clean way to handle theResult<T>type fromRA.Utilities.Core. - Use the
Matchmethod on aResultto map success outcomes toSuccessResponse.Ok()and failure outcomes directly to an exception that the middleware will handle.
- The
-
Comprehensive Documentation:
- The
README.mdhas been completely rewritten to provide clear, step-by-step instructions and usage examples for all major features.
- The
🚀 Getting Started
Register the services in your Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRaExceptionHandling();
builder.Services.AddEndpoints();
var app = builder.Build();
app.UseRaExceptionHandling();
app.MapEndpoints();