I'm currently working with a minimal API in C# and I'm using various extension methods to configure endpoints. In my specific case, I have an endpoint defined like this:
app.MapGet("/newweatherforecast", () =>
{
var forecast = Enumerable.Range(1, 10).Select(index =>
new WeatherForecastNew
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55)
))
.ToArray();
return forecast;
})
.AddFeatureFlag("NewGetWeatherForecast")
.WithName("NewGetWeatherForecast")
.WithOpenApi();
The AddFeatureFlag
method is a custom extension method that ultimately calls my FeatureFlagEndpoint
filter, which looks like this:
public static class FeatureFlagFilterEndpointExtensions
{
public static TBuilder AddFeatureFlag<TBuilder>(this TBuilder builder, string _endpointsName) where TBuilder : IEndpointConventionBuilder
{
builder.AddEndpointFilter(new FeatureFlagEndpoint(_endpointsName));
return builder;
}
}
public class FeatureFlagEndpoint : IEndpointFilter
{
private readonly string _endpointsName;
public FeatureFlagEndpoint(string endpointFilter)
{
_endpointsName = endpointFilter;
}
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
var featureFlag = context.HttpContext.RequestServices.GetRequiredService<IFeatureManager>();
var endpointMethodName = _endpointsName; // Here, I want to retrieve the calling endpoint name using the context.
// I intend to obtain the name of the calling endpoint, accessible through the context. However, I'm unsure about the precise approach to achieve this.
// Once I manage to accomplish this, it will simplify case handling and remove the need for an additional parameterized extension method.
// ...
}
}
In the FeatureFlagEndpoint
class, I'm trying to get the name of the calling endpoint using the context
, but I'm unsure about the exact method to achieve this. Once I successfully retrieve this information, I believe it will lead to simpler case management and eliminate the necessity for the extra parameterized extension method.
Could someone provide guidance on how to retrieve the name of the calling endpoint using the provided context
or suggest an alternative approach to achieve the same result?