dotnet / aspnetcore Public
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 support for endpoint filters #37853
Comments
We met last week to review a design for this last week. The text of the design is below. There's some open questions that we need to follow-up on but for now we are moving forward with implementation primarily in two phases:
Endpoint Filters DesignDate: January 27, 2022 SummaryFilters are MVC construct that allow developers to. Unlike middlewares, filters run after the routing middleware and have access to additional data that middlewares don’t. While middlewares operate on the Filters, and particularly
Goals
Non-goals
Risks/Unknowns
Proposed Design Walkthrough
public class UnsupportedContentTypeFilter : IEndpointFilter
{
public override ValueTask<object> RunAsync(IEndpointFilterContext context, Func<IEndpointFilterContext, ValueTask<object?>> next)
{
// Assume last argument is some Boolean flag
if (context.Parameters.Last())
{
var result = await next(context);
if (result.ContentType != "application/json")
{
return new UnsupportedMediaTypeResult();
}
return result;
}
}
}
// Option 1
app.MapGet("/todos/{id}", (int id) => new Todo(id, $"Task {id}"))
.AddFilter<UnsupportedContentTypeFilter>();
// Option 2
app
.AddFilter<UnsupportedContentTypeFilter>()
.MapGet("/todos/{id}", (int id) => new Todo(id, $"Task {id}"));
// Option 2.5
app.MapGet("/root", () => "Hello from root").AddFilter<UnsupportedContentTypeFilter>()
var rootGroup1 = app.RouteGroup().AddFilter<UnsupportedContentTypeFilter>;
// Option 3
app
.MapGet(new UnsupportedContentTypeFilter(), "/todos/{id}", (int id) => new Todo(id, $"Task {id}"));
$ http POST http://localhost:5000/todos/1
|
Filters in MVC allow for code to be executed immediately before and after the invocation of action methods, allowing for inspection and modification of input parameters before execution, or even completely short-circuiting the action invocation altogether, along with inspection and modification of action method results, or even completely replacing the result, before they are executed.
There is no equivalent of this today when composing request handlers using raw endpoints however, e.g. via
app.MapGet
, etc. This issue is proposing adding support for filters as a new intrinsic in the request pipeline. We could consider having MVC support running filters registered via this new mechanism in addition to its own filters feature.There are two types of filter scenarios to consider:
Func<int, string, IResult>
. This scenario would require access to theBoth scenarios involve each filter invocation getting passed the delegate representing the next filter in the pipeline, such that they can decide whether or not to invoke the next filter or not.
Generic filter example
WIP, more to come
Scratch
The text was updated successfully, but these errors were encountered: