Skip to main content

RA.Utilities.Api

· 2 min read
Redon Alla
.NET, React, Angular Developer

Version 10.0.0-rc.2

NuGet version

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 IExceptionHandler implementation 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.
  • Endpoint Registration Helpers (AddEndpoints & MapEndpoints):

    • Provides a clean pattern for organizing API endpoints into separate files using the IEndpoint interface.
    • Keeps Program.cs clean and maintainable by automatically discovering and registering all endpoint implementations in your project.
  • Standardized Success Response Helpers (SuccessResponse):

    • Added a new static SuccessResponse class 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.
  • Seamless Result<T> Integration:

    • The SuccessResponse helpers and the exception handling middleware work together to provide a clean way to handle the Result<T> type from RA.Utilities.Core.
    • Use the Match method on a Result to map success outcomes to SuccessResponse.Ok() and failure outcomes directly to an exception that the middleware will handle.
  • Comprehensive Documentation:

    • The README.md has been completely rewritten to provide clear, step-by-step instructions and usage examples for all major features.

🚀 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();