ModernMediator.AspNetCore
2.2.1
dotnet add package ModernMediator.AspNetCore --version 2.2.1
NuGet\Install-Package ModernMediator.AspNetCore -Version 2.2.1
<PackageReference Include="ModernMediator.AspNetCore" Version="2.2.1" />
<PackageVersion Include="ModernMediator.AspNetCore" Version="2.2.1" />
<PackageReference Include="ModernMediator.AspNetCore" />
paket add ModernMediator.AspNetCore --version 2.2.1
#r "nuget: ModernMediator.AspNetCore, 2.2.1"
#:package ModernMediator.AspNetCore@2.2.1
#addin nuget:?package=ModernMediator.AspNetCore&version=2.2.1
#tool nuget:?package=ModernMediator.AspNetCore&version=2.2.1
ModernMediator.AspNetCore
ASP.NET Core integration for ModernMediator. Provides compile-time Minimal API endpoint generation from request handlers decorated with the [Endpoint] attribute, plus an IHttpContextAccessor-backed ICurrentUserAccessor for handlers that need authenticated user context.
Installation
dotnet add package ModernMediator.AspNetCore
The package transitively includes ModernMediator and ModernMediator.AspNetCore.Generators. You do not need to install those separately.
Setup
In Program.cs:
using ModernMediator;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddModernMediator(config =>
{
config.RegisterServicesFromAssemblyContaining<Program>();
});
builder.Services.AddModernMediatorAspNetCore();
var app = builder.Build();
app.MapMediatorEndpoints();
app.Run();
AddModernMediatorAspNetCore() registers IHttpContextAccessor, which HttpContextCurrentUserAccessor depends on.
MapMediatorEndpoints() is generated by ModernMediator.AspNetCore.Generators at compile time. It registers every [Endpoint]-decorated request handler as a Minimal API route.
Defining endpoints
Decorate request types with [Endpoint]. The constructor takes the route first and the HTTP method second (defaults to "POST"):
using ModernMediator;
using ModernMediator.AspNetCore;
[Endpoint("/orders/{id:int}", "GET")]
public sealed record GetOrderQuery(int Id) : IRequest<OrderResult>;
public sealed class GetOrderHandler : IRequestHandler<GetOrderQuery, OrderResult>
{
public Task<OrderResult> Handle(GetOrderQuery query, CancellationToken cancellationToken)
{
// ...
}
}
Valid HTTP methods: "GET", "POST", "PUT", "DELETE", "PATCH". GET endpoints bind route and query parameters into the request type via [AsParameters]; non-GET endpoints bind the request body via [FromBody]. The generator dispatches each request through IMediator.Send.
See the core ModernMediator README for the full attribute reference and parameter binding rules.
Current user access
HttpContextCurrentUserAccessor exposes the authenticated user's identity to handlers via the ICurrentUserAccessor interface (defined in the core ModernMediator package). It surfaces two pre-extracted values:
UserId: theClaimTypes.NameIdentifierclaimUserName:IIdentity.Name
Both return null when no HTTP context is available or the request is unauthenticated.
HttpContextCurrentUserAccessor is registered automatically when you opt into the audit pipeline with UseHttpContextIdentity():
builder.Services.AddModernMediator(cfg => cfg
.AddAudit<MyAuditWriter>(o => o.UseHttpContextIdentity(builder.Services)));
If you need ICurrentUserAccessor outside the audit pipeline, register it explicitly:
builder.Services.AddSingleton<ICurrentUserAccessor, HttpContextCurrentUserAccessor>();
Then inject it into a handler:
public sealed class GetMyOrdersHandler : IRequestHandler<GetMyOrdersQuery, IReadOnlyList<OrderResult>>
{
private readonly ICurrentUserAccessor _currentUser;
public GetMyOrdersHandler(ICurrentUserAccessor currentUser)
{
_currentUser = currentUser;
}
public Task<IReadOnlyList<OrderResult>> Handle(GetMyOrdersQuery query, CancellationToken cancellationToken)
{
var userId = _currentUser.UserId;
// ...
}
}
See also
- Core ModernMediator README for the full library reference
- ModernMediator.AspNetCore.Generators, the source generators (shipped transitively)
License
MIT. See LICENSE in the repository root.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
-
net8.0
- ModernMediator (>= 2.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.