-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Global endpoint filter (edit: and other conventions?) #43237
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
Comments
The way to do this is with groups. Make a global group and stick all of the endpoints in it. In the future, we may consider a default convention builder on the WebApplication object. var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var routes = app.MapGroup("");
routes.AddEndpointFilter((context, next) =>
{
return next(context);
});
routes.MapGet("/", () => "Hello World!");
app.Run(); |
Thanks @davidfowl |
I love using an empty group prefix for this! Support for gobal metadata and filters was the main reason I figured it was worth allowing empty group prefixes. I think we can do better though. What do we think about having Background and MotivationSee above. Proposed APInamespace Microsoft.AspNetCore.Builder; namespace Microsoft.AspNetCore.Http;
- public sealed class WebApplication : IHost, IApplicationBuilder, IEndpointRouteBuilder, IAsyncDisposable
+ public sealed class WebApplication : IHost, IApplicationBuilder, IEndpointRouteBuilder, IEndpointConventionBuilder, IAsyncDisposable Usage Examplesvar app = WebApplication.Create(args);
app.AddEndpointFilter((context, next) =>
{
return next(context);
});
app.MapGet("/", () => "Hello World!");
app.Run(); Alternative DesignsLeave it as is. You can always create an route group with an empty prefix if you want this functionality as @davidfowl suggests: var app = WebApplication.Create(args);
var routes = app.MapGroup("");
routes.AddEndpointFilter((context, next) =>
{
return next(context);
});
routes.MapGet("/", () => "Hello World!");
app.Run(); RisksIt would be too noisy to have too many extension methods that all work on |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
The |
I don't like this proposal I think top level intellisense will be too noisy. Routing methods are usually Map*, middleware methods are Use*, these conventions could don't have a consistent naming scheme. We use With*, Add*, Require*. I'm worried about jumbled intellisense and naming conflicts. |
Here's an alternative proposal: namespace Microsoft.AspNetCore.Http;
public sealed class WebApplication
{
+ public RouteGroupBuilder Routes { get; }
} We expose a default top level app.Routes.MapGet("/", () => "Hello"); is the same as: app.MapGet("/", () => "Hello"); Adding global conventions is then: app.Routes.AddEndpointFilter((context, next) =>
{
return next(context);
}); It also solves some of the complaints we've had in this area: |
Another alternative is adding a namespace Microsoft.AspNetCore.Http;
public sealed class WebApplication
{
+ public IEndpointConventionBuilder Conventions { get; }
} Usage: app.MapGet("/", () => "Hello");
app.Conventions.AddEndpointFilter((context, next) =>
{
return next(context);
}); We've said that we don't want APIs targeting |
Or we could do a hybrid approach, so we still allow you to delineate between the namespace Microsoft.AspNetCore.Http;
public sealed class WebApplication
{
+ public IEndpointRouteBuilder Routes { get; }
+ public IEndpointConventionBuilder Conventions { get; }
} Usage: app.Routes.MapGet("/", () => "Hello");
app.Conventions.AddEndpointFilter((context, next) =>
{
return next(context);
}); |
I considered this as well but I figured 2 birds with one stone (see the blog posts). Is there any reason why we can't expose a sealed type? Are you predicting extension methods targeting this? |
I just read them before I suggested the hybrid proposal that addresses the concern of both blogposts. I think it's better because it's not adding yet a new property that arguably implements too many interfaces on a single type.
I hope not. If we were willing to make a major change to the route group APIs in the 11th hour of .NET 7 we could change namespace Microsoft.AspNetCore.Routing;
- public sealed class RouteGroupBuilder : IEndpointRouteBuilder, IEndpointConventionBuilder
+ public sealed class RouteGroupBuilder
{
+ public IEndpointRouteBuilder Routes { get; }
+ public IEndpointConventionBuilder Conventions { get; }
} With this API change, the current workaround for global convetions goes from being like: var app = WebApplication.Create(args);
var routes = app.MapGroup("");
routes.AddEndpointFilter((context, next) =>
{
return next(context);
});
routes.MapGet("/", () => "Hello World!");
app.Run(); To something like this: var app = WebApplication.Create(args);
var group = app.MapGroup("");
group.Conventions.AddEndpointFilter((context, next) =>
{
return next(context);
});
group.Routes.MapGet("/", () => "Hello World!");
app.Run(); @andrewlock and @khalidabuhakmeh do either of you have any thoughts on this? |
API Review Notes:
We feel we're going to need customer feedback to see if we really want to do any of the above proposals. We do not think we want Most of the other suggestions are additive and could be done in a later release. |
I mean, I have definite thoughts if we could turn back time and have For the hybrid approach: RE splitting For me the top-level Personally, I think the original workaround, of creating a "top-level route" is the best option given the existing constraints |
I agree with @andrewlock in the sense that The thing I like about ASP.NET Core is the shared model, and that includes the shared mental model across all the approaches. Trying to be too different might drive people to "pick sides", rather than realizing the strength is in flowing between Razor Pages, Minimal API endpoints, MVC, and SignalR. |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
Since we introduced the endpoint filter feature, is there also a global endpoint filter available?
So that we could avoid registering the endpoint filter for each endpoint.
Describe the solution you'd like
Maybe likes MVC global filters, and maybe group endpoint filter for a
Group
(MapGroup
) likes the filter for controllerAdditional context
No response
The text was updated successfully, but these errors were encountered: