Skip to main content

RA.Utilities.Api

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

Version 10.0.2

Date Badge NuGet version

Add CreatedAtRoute methods to SuccessResponse for enhanced API response handling.

Version 10.0.1

Date Badge NuGet version

✨ New Features: FluentValidation Integration for Minimal APIs

This release introduces a comprehensive integration with FluentValidation for ASP.NET Core Minimal APIs. You can now effortlessly validate incoming requests and automatically reflect those validation rules in your OpenAPI (Swagger) documentation. This creates a single source of truth for your validation logic, improving maintainability and providing a better experience for your API consumers.

1. Automatic Request Validation

A new endpoint filter has been introduced that automatically validates API request models. By simply adding .Validate<TModel>() to your endpoint definition, you can ensure that all incoming data is valid before your handler logic is executed. If validation fails, a standardized 400 Bad Request response is returned with detailed error information.

How to use it:

Chain the .Validate<TModel>() extension method to your endpoint registration.

// In your endpoint definition
app.MapPost("/users", (CreateUserRequest user) => {
// Handler logic here...
return SuccessResponse.Created(user);
})
.Validate<CreateUserRequest>(); // This enables automatic validation

This leverages the ValidationEndpointFilter<TModel>, which resolves your IValidator<TModel> implementations from the dependency injection container and executes them.


Version 10.0.0

Date Badge NuGet version

Changed project version from a release candidate to final version 10.0.0 for production readiness.

Revised XML documentation comments to improve clarity and detail. Improved the documentation in IEndpoint to clarify its purpose in grouping related API endpoints. Adjusted parameter and return type descriptions in EndpointExtensions for better understanding of default assembly behavior. Enhanced comments in SuccessResponse to explicitly state response wrapping behavior.

Version 10.0.0-rc.2

Date Badge 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();