-
Notifications
You must be signed in to change notification settings - Fork 111
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
Add a result filter for razor pages #34
Comments
Just as a follow-up, I hacked up a proof of concept to show this would work. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class FeatureToggleAttribute : FeatureGateAttribute
{
public FeatureToggleAttribute(params string[] features)
: this(RequirementType.All, features)
{
}
public FeatureToggleAttribute(RequirementType requirementType, params string[] features)
: base(requirementType, features)
{
}
public FeatureToggleAttribute(RequirementType requirementType, params object[] features)
: base(requirementType, features)
{
}
public FeatureToggleAttribute(params object[] features)
: this(RequirementType.All, features)
{
}
public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
var fm = context.HttpContext.RequestServices.GetRequiredService<IFeatureManagerSnapshot>();
bool flag;
if (RequirementType == RequirementType.All)
flag = await Features.All<string>((Func<string, Task<bool>>) (async feature =>
await fm.IsEnabledAsync(feature).ConfigureAwait(false)));
else
flag = await Features.Any<string>((Func<string, Task<bool>>) (async feature =>
await fm.IsEnabledAsync(feature).ConfigureAwait(false)));
if (flag)
{
await next().ConfigureAwait(false);
}
else
{
context.HttpContext.Response.StatusCode = 404;
await context.HttpContext.Response.CompleteAsync();
}
}
} This code makes use of internal classes within this repo that I didn't copy. But it shows that the basic idea would work. |
+1 for this as this is exactly what we were looking at as a solution in one of our apps .. |
I spent some time looking into this because I wanted to provide some out of the box support for Razor Pages in the same way that we provide it for MVC controllers. There is an unfortunate difference between supporting controllers via I will look a bit more to see if it makes sense to provide a global |
Good point on the result execution filter being too late. Could |
@jimmyca15 IMO, this would really increase the value of this library and shouldn't be difficult to implement. We already implemented a a very simple IAsyncPageFilter with similar implementation provided by @haacked and it's nicely working:
|
services
.AddRazorPages(options =>
{
...
options.Conventions.AddAreaFolderApplicationModelConvention("Admin", "/Customers", cOption =>
{
cOption.Filters.Add(new RazorPageFeatureGate(ApplicationFeatureFlags.ManageCustomer));
});
}) Apply feature gate filter to area folder |
Calling context.HttpContext.Response.StatusCode = 404;
await context.HttpContext.Response.CompleteAsync().ConfigureAwait(false); Wouldn't it be better to avoid page execution by setting the context.Result = new NotFoundResult(); |
I suggest you make a PR from this. Worked like a charm ! Thanks for sharing |
Since page filters and action filters are executed in isolation we can add this new functionality in the existing |
This has been released in Microsoft.FeatureManagement.AspNetCore 2.5.0 Documentation update is in the works. |
Right now, there's no nice declarative way (that I could find) to add a feature flag to a razor page.
Doesn't work because
FeatureGateAttribute
only overridesOnActionExecutionAsync
. I haven't tested this myself, but I believe if you also implementOnResultExecutingAsync
you'll cover Razor pages. At least according to the docs Filter methods for Razor Pages in ASP.NET CoreThe text was updated successfully, but these errors were encountered: