RA.Utilities.Api.Middlewares
10.0.0
Prefix Reserved
dotnet add package RA.Utilities.Api.Middlewares --version 10.0.0
NuGet\Install-Package RA.Utilities.Api.Middlewares -Version 10.0.0
<PackageReference Include="RA.Utilities.Api.Middlewares" Version="10.0.0" />
<PackageVersion Include="RA.Utilities.Api.Middlewares" Version="10.0.0" />
<PackageReference Include="RA.Utilities.Api.Middlewares" />
paket add RA.Utilities.Api.Middlewares --version 10.0.0
#r "nuget: RA.Utilities.Api.Middlewares, 10.0.0"
#:package RA.Utilities.Api.Middlewares@10.0.0
#addin nuget:?package=RA.Utilities.Api.Middlewares&version=10.0.0
#tool nuget:?package=RA.Utilities.Api.Middlewares&version=10.0.0
RA.Utilities.Api.Middlewares
RA.Utilities.Api.Middlewares provides a collection of useful ASP.NET Core middlewares to solve common cross-cutting concerns. It includes a high-performance middleware for logging HTTP requests/responses and another for enforcing the presence of required headers like X-Request-Id to ensure traceability.
📚 Table of Contents
- Getting started
- Dependencies
- Features
- HTTP Logging Middleware
- Default Headers Middleware
- Additional documentation
- Contributing
- License
Getting started
Install the package via the .NET CLI:
dotnet add package RA.Utilities.Api.Middlewares
Or through the NuGet Package Manager in Visual Studio.
🔗 Dependencies
RA.Utilities.Core.ConstantsRA.Utilities.Logging.SharedRA.Utilities.Api.ResultsMicrosoft.AspNetCore.AppMicrosoft.IO.RecyclableMemoryStream
Features
1. HTTP Logging Middleware
The [HttpLoggingMiddleware] captures and logs the details of each HTTP request and response, including headers, body, and status codes. It is designed for performance and uses the structured logging models from RA.Utilities.Logging.Shared.
Usage
Step 1: Register the middleware services in Program.cs
Call AddHttpLoggingMiddleware() in your service configuration. You can also provide options to customize its behavior, such as excluding certain paths from logging.
// Program.cs
using RA.Utilities.Api.Middlewares.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add the logging middleware services
builder.Services.AddHttpLoggingMiddleware(options =>
{
options.ExcludePaths.Add("/swagger");
options.ExcludePaths.Add("/health");
});
var app = builder.Build();
Step 2: Add the middleware to the pipeline
Place app.UseMiddleware<HttpLoggingMiddleware>() early in your middleware pipeline to ensure it captures the full request/response cycle.
// Program.cs (continued)
app.UseMiddleware<HttpLoggingMiddleware>();
app.MapGet("/", () => "Hello World!");
app.Run();
2. Default Headers Middleware
The DefaultHeadersMiddleware enforces the presence of the X-Request-Id header on all incoming requests. This is crucial for distributed tracing and correlating logs across multiple services.
How it works:
- If the
X-Request-Idheader is present, it is added to the response headers for the client to see. - If the header is missing, the middleware immediately short-circuits the request and returns an HTTP 400 Bad Request with a standardized error response. It also generates a new
X-Request-Idand adds it to the response for traceability.
Usage
Step 1: Register the middleware services in Program.cs
Call AddDefaultHeadersMiddleware() in your service configuration.
You can also provide options to customize its behavior, such as excluding certain paths from header validation.
// Program.cs
using RA.Utilities.Api.Middlewares.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDefaultHeadersMiddleware(options =>
{
options.PathsToIgnore.Add("/swagger");
options.PathsToIgnore.Add("/health");
});
var app = builder.Build();
Step 2: Add the middleware to the pipeline
Place app.UseMiddleware<DefaultHeadersMiddleware>() right after routing to ensure it runs for all API endpoints.
// Program.cs (continued)
app.UseRouting();
app.UseMiddleware<DefaultHeadersMiddleware>();
app.MapControllers();
app.Run();
A request without the X-Request-Id header will receive a response like this:
{
"responseCode": 400,
"responseType": "BadRequest",
"responseMessage": "One or more validation errors occurred.",
"result": [
{
"propertyName": "X-Request-Id",
"errorMessage": "Header 'X-Request-Id' is required.",
"attemptedValue": null,
"errorCode": "NotNullValidator"
}
]
}
Additional documentation
For more information on how this package fits into the larger RA.Utilities ecosystem, please see the main repository documentation.
Contributing
Contributions are welcome! If you have a suggestion or find a bug, please open an issue to discuss it first.
Pull Request Process
- Fork the Repository: Start by forking the RA.Utilities repository.
- Create a Branch: Create a new branch for your feature or bug fix from the
mainbranch. Please use a descriptive name (e.g.,feature/add-auth-helperorfix/middleware-bug). - Make Your Changes: Write your code, ensuring it adheres to the project's coding style.
- Add Tests: Add or update unit tests for your changes to ensure correctness and prevent regressions.
- Update Documentation: Add or update XML documentation for any new public APIs. If you are adding new functionality, please update the relevant
README.mdfile. - Verify Locally: Ensure the solution builds and all tests pass locally before submitting.
- Submit a Pull Request: Push your branch to your fork and open a pull request to the
mainbranch of the original repository. Provide a clear description of the changes you have made.
Coding Standards
- Style: Follow the coding conventions defined in the
.editorconfigfile at the root of the repository. The build is configured to enforce these styles. - Documentation: Ensure all public members are documented with clear XML comments.
- Commit Messages: Consider using Conventional Commit messages (e.g.,
feat:,fix:,docs:) to keep the commit history clean and informative. - Scope: Keep changes focused. A pull request should address a single feature or bug.
Thank you for contributing!
📜 License
This project is licensed under the MIT License. See the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
- RA.Utilities.Api.Results (>= 10.0.0)
- RA.Utilities.Core.Constants (>= 10.0.0)
- RA.Utilities.Logging.Shared (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.0.0 | 220 | 11/23/2025 |
| 10.0.0-rc.3 | 378 | 11/19/2025 |
| 10.0.0-rc.2 | 120 | 10/31/2025 |