Closed DumboJetEngine closed 2 months ago
You can use the RouteGroupBuilder
with MapCarter()
as it inherits from IEndpointRouteBuilder
. For example,
...
var global = app.MapGroup(string.Empty).AddEndpointFilter<MyGlobalFilter>();
global.MapCarter();
app.Run();
public class MyGlobalFilter(ILoggerFactory loggerFactory) : IEndpointFilter {
private readonly ILogger<MyGlobalFilter> logger = loggerFactory.CreateLogger<MyGlobalFilter>();
public async ValueTask<object> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
this.logger.LogInformation("Before Request...");
var result = await next(context);
var content = JsonSerializer.Serialize(result);
this.logger.LogInformation(content);
this.logger.LogInformation("After Request...");
return result;
}
}
Which gave the following log output.
@ritasker This is awesome! Thanks.
Is it possible to apply a global filter with Carter that applies to all endpoints of all Carter modules?
I need to post-process each endpoint's return values and modify the response accordingly. One way to do this with asp.net core minimal APIs, is to apply the filter to a
MapGroup
usingMapGroup().AddEndpointFilter<GlobalFilter>();
and then define all endpoints on this group.But since
ICarterModule
works withIEndpointRouteBuilder
and notRouteGroupBuilder
, I guess it is not possible to apply the filter automatically to all Carter modules?