-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add support for endpoint filters in minimal APIs #40491
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14eed74
Add support for endpoint filters in minimal APIs
captainsafia c981930
Fix build and react to peer review
captainsafia be5a2ac
Fixing failing tests and address feedback
captainsafia 2f8a5b6
Don't execute handler after filter if StatusCode >= 400
captainsafia 70fbdab
Rebase and fix new test
captainsafia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Http; | ||
|
||
/// <summary> | ||
/// Provides an interface for implementing a filter targetting a route handler. | ||
/// </summary> | ||
public interface IRouteHandlerFilter | ||
{ | ||
/// <summary> | ||
/// Implements the core logic associated with the filter given a <see cref="RouteHandlerFilterContext"/> | ||
/// and the next filter to call in the pipeline. | ||
/// </summary> | ||
/// <param name="context">The <see cref="RouteHandlerFilterContext"/> associated with the current request/response.</param> | ||
/// <param name="next">The next filter in the pipeline.</param> | ||
/// <returns>An awaitable result of calling the handler and apply | ||
/// any modifications made by filters in the pipeline.</returns> | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ValueTask<object?> InvokeAsync(RouteHandlerFilterContext context, Func<RouteHandlerFilterContext, ValueTask<object?>> next); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
#nullable enable | ||
*REMOVED*abstract Microsoft.AspNetCore.Http.HttpResponse.ContentType.get -> string! | ||
Microsoft.AspNetCore.Http.EndpointMetadataCollection.GetRequiredMetadata<T>() -> T! | ||
Microsoft.AspNetCore.Http.RouteHandlerFilterContext.RouteHandlerFilterContext(Microsoft.AspNetCore.Http.HttpContext! httpContext, params object![]! parameters) -> void | ||
Microsoft.AspNetCore.Http.IRouteHandlerFilter.InvokeAsync(Microsoft.AspNetCore.Http.RouteHandlerFilterContext! context, System.Func<Microsoft.AspNetCore.Http.RouteHandlerFilterContext!, System.Threading.Tasks.ValueTask<object?>>! next) -> System.Threading.Tasks.ValueTask<object?> | ||
Microsoft.AspNetCore.Http.Metadata.IFromFormMetadata | ||
Microsoft.AspNetCore.Http.Metadata.IFromFormMetadata.Name.get -> string? | ||
Microsoft.AspNetCore.Routing.RouteValueDictionary.RouteValueDictionary(Microsoft.AspNetCore.Routing.RouteValueDictionary? dictionary) -> void | ||
Microsoft.AspNetCore.Routing.RouteValueDictionary.RouteValueDictionary(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object?>>? values) -> void | ||
Microsoft.AspNetCore.Routing.RouteValueDictionary.RouteValueDictionary(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string?>>? values) -> void | ||
abstract Microsoft.AspNetCore.Http.HttpResponse.ContentType.get -> string? | ||
Microsoft.AspNetCore.Http.Metadata.ISkipStatusCodePagesMetadata | ||
Microsoft.AspNetCore.Http.RouteHandlerFilterContext | ||
Microsoft.AspNetCore.Http.RouteHandlerFilterContext.HttpContext.get -> Microsoft.AspNetCore.Http.HttpContext! | ||
Microsoft.AspNetCore.Http.RouteHandlerFilterContext.Parameters.get -> System.Collections.Generic.IList<object?>! | ||
Microsoft.AspNetCore.Http.IRouteHandlerFilter |
35 changes: 35 additions & 0 deletions
35
src/Http/Http.Abstractions/src/RouteHandlerFilterContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||
// Licensed to the .NET Foundation under one or more agreements. | ||||||
// The .NET Foundation licenses this file to you under the MIT license. | ||||||
|
||||||
namespace Microsoft.AspNetCore.Http; | ||||||
|
||||||
/// <summary> | ||||||
/// Provides an abstraction for wrapping the <see cref="HttpContext"/> and parameters | ||||||
/// provided to a route handler. | ||||||
/// </summary> | ||||||
public class RouteHandlerFilterContext | ||||||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ | ||||||
/// <summary> | ||||||
/// Creates a new instance of the <see cref="RouteHandlerFilterContext"/> for a given request. | ||||||
/// </summary> | ||||||
/// <param name="httpContext">The <see cref="HttpContext"/> associated with the current request.</param> | ||||||
/// <param name="parameters">A list of parameters provided in the current request.</param> | ||||||
public RouteHandlerFilterContext(HttpContext httpContext, params object[] parameters) | ||||||
{ | ||||||
HttpContext = httpContext; | ||||||
Parameters = parameters; | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// The <see cref="HttpContext"/> associated with the current request being processed by the filter. | ||||||
/// </summary> | ||||||
public HttpContext HttpContext { get; } | ||||||
|
||||||
/// <summary> | ||||||
/// A list of parameters provided in the current request to the filter. | ||||||
/// <remarks> | ||||||
/// This list is not read-only to premit modifying of existing parameters by filters. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Next PR:
Suggested change
|
||||||
/// </remarks> | ||||||
/// </summary> | ||||||
public IList<object?> Parameters { get; } | ||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#nullable enable | ||
Microsoft.Extensions.DependencyInjection.RouteHandlerJsonServiceExtensions | ||
static Microsoft.Extensions.DependencyInjection.RouteHandlerJsonServiceExtensions.ConfigureRouteHandlerJsonOptions(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action<Microsoft.AspNetCore.Http.Json.JsonOptions!>! configureOptions) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! | ||
Microsoft.AspNetCore.Http.RequestDelegateFactoryOptions.RouteHandlerFilters.get -> System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Http.IRouteHandlerFilter!>? | ||
Microsoft.AspNetCore.Http.RequestDelegateFactoryOptions.RouteHandlerFilters.init -> void |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.