ASP.NET Core – Is it possible to apply a filter for all minimal API endpoints?

  Kiến thức lập trình

I want to apply a filter to all asp.net core minimal API endpoints.

The reason I want to do this is to post-process the return values of the endpoint. Something like this:

    public class GlobalFilter : IEndpointFilter
    {
        private ILogger _logger;

        public GlobalValidationFilter(ILogger<GlobalValidationFilter> logger)
        {
            _logger = logger;
        }

        public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext invocationContext, EndpointFilterDelegate next)
        {
            try
            {
                var res = await next(invocationContext);
                var result = res as Result;
                if(result is not null)
                {
                    // Here, access the `result` members and modify the return value accordingly...
                }

                return Results.Problem();
            }
            catch (Exception ex)
            {
                return Results.Problem(ex.ToString());
            }
        }
    }
            app
                .MapPost("api/document/getById", async ([FromBody] GetDocumentRequest request) =>
                {
                    var result = ...result-producing call...;
                    return result;
                })
                .AddEndpointFilter<GlobalFilter>() // Manually assigning the filter.
                ...
                ...;

This appears to work, but I would have to remember to set the filter manually every time.
Is there a way to apply this filter to all endpoints automatically?

Alternatively, is it possible to achieve the same thing with another method? From what I have seen, using a middleware does not allow you to inspect the endpoint return values, but only handle thrown exceptions.

LEAVE A COMMENT