-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: AddEndpointFilter for Minimal API's #253
Comments
Hello @timdeschryver , Looking into it a little now. From my understanding, the filter itself needs to handle the true and false case. Today, I believe it could be implemented like: .AddEndpointFilter(async (context, next) =>
{
if (await featureManager.IsEnabledAsync(FeatureName))
{
// true;
} else {
// false;
}
}); If you care about context, similarly to if (await featureManager.IsEnabledAsync(FeatureName, new TargetingContext{ User: context.username }); I hope those work for you. I don't yet see what a helper method could offer here. We could automatically handle the false case, but that would mean we're forcing everyone to that false result. If we pick 404, that might not be ideal for everyone's situation. Let me know if you have something different in mind! |
Thanks for the fast response @rossgrambo. Feel free to close this issue if this doesn't add much value. |
Ah, I see what you mean. I'll bring it up to the team as a suggestion. Thanks @timdeschryver! |
add EndpointFeatureMiddleware public class EndpointFeatureMiddleware
{
private readonly RequestDelegate _next;
private readonly IFeatureManager _featureManager;
public EndpointFeatureMiddleware(RequestDelegate next, IFeatureManager featureManager)
{
_next = next;
_featureManager = featureManager;
}
public async Task InvokeAsync(HttpContext context)
{
if (context.GetEndpoint() is { } endpoint)
{
if (endpoint.Metadata.GetMetadata<FeatureGateAttribute>() is { } metadata)
{
var features = metadata.Features;
//只要有一个特性开启就可以:
if (metadata.RequirementType == RequirementType.Any)
{
foreach (var feature in features)
{
if (await _featureManager.IsEnabledAsync(feature))
{
await _next(context);
return;
}
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
return;
}
}
//所有特性都必须开启:
else
{
foreach (var feature in features)
{
if (!await _featureManager.IsEnabledAsync(feature))
{
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
return;
}
}
}
}
}
await _next(context);
}
} app.UseMiddleware<EndpointFeatureMiddleware>(); then test: app.MapGet("feature", () =>
{
return Results.Content("hello world");
}).WithMetadata(new FeatureGateAttribute("myfeature")); It works fine :) |
Hi, I was wondering if there are any plans to introduce an endpoint filter that can be used with Minimal API's.
While this library already provides the
FeatureGate
attribute for Controller based API's or MVC applications, I couldn't find anything about a helper method for Minimal API's.It could be missing because this library existed before, ASP.NET Core introduced the new extension method
AddEndpointFilter
to customize a route handler in v7.I think it can be a great addition to this library.
Thanks in advance.
The text was updated successfully, but these errors were encountered: