2

In the IEndpointFilter would like to get the route used.

app.MapGet("/get/{productId:guid}", ......


public class MyFilter : IEndpointFilter
{
    public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
    {

    //Getting it here /get/{productId:guid}
    return await next.Invoke(context);
    }
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
Kazmir
  • 25
  • 4

1 Answers1

1

You can use HttpContext.GetEndpoint() method and then check if endpoint is RouteEndpoint:

class MyFilter : IEndpointFilter
{
    public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
    {
        var endpoint = context.HttpContext.GetEndpoint();
        if (endpoint is RouteEndpoint re1)
        {
            Console.WriteLine(re1.RoutePattern.RawText);
        }

        return await next(context);
    }
}

Or do manually what GetEndpoint does itself:

var endpointFeature = context.HttpContext.Features.Get<IEndpointFeature>();
if (endpointFeature?.Endpoint is RouteEndpoint re)
{
    Console.WriteLine(re.RoutePattern.RawText);
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132