Skip to content

Make route handler filters work for MVC controllers #42557

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 7 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace Microsoft.AspNetCore.Builder;
/// </summary>
public abstract class EndpointBuilder
{
/// <summary>
/// Gets the list of filters that apply to this endpoint.
/// </summary>
public IList<Func<EndpointFilterFactoryContext, EndpointFilterDelegate, EndpointFilterDelegate>> FilterFactories { get; } = new List<Func<EndpointFilterFactoryContext, EndpointFilterDelegate, EndpointFilterDelegate>>();

/// <summary>
/// Gets or sets the delegate used to process requests for the endpoint.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ abstract Microsoft.AspNetCore.Http.EndpointFilterInvocationContext.GetArgument<T
abstract Microsoft.AspNetCore.Http.EndpointFilterInvocationContext.HttpContext.get -> Microsoft.AspNetCore.Http.HttpContext!
Microsoft.AspNetCore.Builder.EndpointBuilder.ApplicationServices.get -> System.IServiceProvider!
Microsoft.AspNetCore.Builder.EndpointBuilder.ApplicationServices.set -> void
Microsoft.AspNetCore.Builder.EndpointBuilder.FilterFactories.get -> System.Collections.Generic.IList<System.Func<Microsoft.AspNetCore.Http.EndpointFilterFactoryContext!, Microsoft.AspNetCore.Http.EndpointFilterDelegate!, Microsoft.AspNetCore.Http.EndpointFilterDelegate!>!>!
Microsoft.AspNetCore.Http.AsParametersAttribute
Microsoft.AspNetCore.Http.AsParametersAttribute.AsParametersAttribute() -> void
Microsoft.AspNetCore.Http.CookieBuilder.Extensions.get -> System.Collections.Generic.IList<string!>!
Expand Down
8 changes: 1 addition & 7 deletions src/Http/Routing/src/Builder/EndpointFilterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,7 @@ public static TBuilder AddEndpointFilterFactory<TBuilder>(this TBuilder builder,
{
builder.Add(endpointBuilder =>
{
if (endpointBuilder is not RouteEndpointBuilder routeEndpointBuilder)
{
return;
}

routeEndpointBuilder.EndpointFilterFactories ??= new();
routeEndpointBuilder.EndpointFilterFactories.Add(filterFactory);
endpointBuilder.FilterFactories.Add(filterFactory);
});

return builder;
Expand Down
10 changes: 9 additions & 1 deletion src/Http/Routing/src/Patterns/RoutePatternFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,15 @@ public static RoutePatternParameterPolicyReference ParameterPolicy(string parame
return ParameterPolicyCore(parameterPolicy);
}

internal static RoutePattern Combine(RoutePattern? left, RoutePattern right)
/// <summary>
/// Creates a <see cref="RoutePattern"/> that combines the specified patterns.
/// </summary>
/// <param name="left">A string representing the first part of the route.</param>
/// <param name="right">A stirng representing the second part of the route.</param>
/// <returns>The combined <see cref="RoutePattern"/>.</returns>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="RoutePatternException"></exception>
public static RoutePattern Combine(RoutePattern? left, RoutePattern right)
{
static IReadOnlyDictionary<string, TValue> CombineDictionaries<TValue>(
IReadOnlyDictionary<string, TValue> leftDictionary,
Expand Down
1 change: 1 addition & 0 deletions src/Http/Routing/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static Microsoft.AspNetCore.Routing.LinkGeneratorRouteValuesAddressExtensions.Ge
static Microsoft.AspNetCore.Routing.LinkGeneratorRouteValuesAddressExtensions.GetPathByRouteValues(this Microsoft.AspNetCore.Routing.LinkGenerator! generator, string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary? values = null, Microsoft.AspNetCore.Http.PathString pathBase = default(Microsoft.AspNetCore.Http.PathString), Microsoft.AspNetCore.Http.FragmentString fragment = default(Microsoft.AspNetCore.Http.FragmentString), Microsoft.AspNetCore.Routing.LinkOptions? options = null) -> string?
static Microsoft.AspNetCore.Routing.LinkGeneratorRouteValuesAddressExtensions.GetUriByRouteValues(this Microsoft.AspNetCore.Routing.LinkGenerator! generator, Microsoft.AspNetCore.Http.HttpContext! httpContext, string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary? values = null, string? scheme = null, Microsoft.AspNetCore.Http.HostString? host = null, Microsoft.AspNetCore.Http.PathString? pathBase = null, Microsoft.AspNetCore.Http.FragmentString fragment = default(Microsoft.AspNetCore.Http.FragmentString), Microsoft.AspNetCore.Routing.LinkOptions? options = null) -> string?
static Microsoft.AspNetCore.Routing.LinkGeneratorRouteValuesAddressExtensions.GetUriByRouteValues(this Microsoft.AspNetCore.Routing.LinkGenerator! generator, string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary! values, string! scheme, Microsoft.AspNetCore.Http.HostString host, Microsoft.AspNetCore.Http.PathString pathBase = default(Microsoft.AspNetCore.Http.PathString), Microsoft.AspNetCore.Http.FragmentString fragment = default(Microsoft.AspNetCore.Http.FragmentString), Microsoft.AspNetCore.Routing.LinkOptions? options = null) -> string?
static Microsoft.AspNetCore.Routing.Patterns.RoutePatternFactory.Combine(Microsoft.AspNetCore.Routing.Patterns.RoutePattern? left, Microsoft.AspNetCore.Routing.Patterns.RoutePattern! right) -> Microsoft.AspNetCore.Routing.Patterns.RoutePattern!
static Microsoft.AspNetCore.Routing.Patterns.RoutePatternFactory.Parse(string! pattern, Microsoft.AspNetCore.Routing.RouteValueDictionary? defaults, Microsoft.AspNetCore.Routing.RouteValueDictionary? parameterPolicies) -> Microsoft.AspNetCore.Routing.Patterns.RoutePattern!
static Microsoft.AspNetCore.Routing.Patterns.RoutePatternFactory.Parse(string! pattern, Microsoft.AspNetCore.Routing.RouteValueDictionary? defaults, Microsoft.AspNetCore.Routing.RouteValueDictionary? parameterPolicies, Microsoft.AspNetCore.Routing.RouteValueDictionary? requiredValues) -> Microsoft.AspNetCore.Routing.Patterns.RoutePattern!
static Microsoft.AspNetCore.Routing.Patterns.RoutePatternFactory.Pattern(Microsoft.AspNetCore.Routing.RouteValueDictionary? defaults, Microsoft.AspNetCore.Routing.RouteValueDictionary? parameterPolicies, System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Routing.Patterns.RoutePatternPathSegment!>! segments) -> Microsoft.AspNetCore.Routing.Patterns.RoutePattern!
Expand Down
4 changes: 0 additions & 4 deletions src/Http/Routing/src/RouteEndpointBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ namespace Microsoft.AspNetCore.Routing;
/// </summary>
public sealed class RouteEndpointBuilder : EndpointBuilder
{
// TODO: Make this public as a gettable IReadOnlyList<Func<RouteHandlerContext, EndpointFilterDelegate, EndpointFilterDelegate>>.
// AddEndpointFilter will still be the only way to mutate this list.
internal List<Func<EndpointFilterFactoryContext, EndpointFilterDelegate, EndpointFilterDelegate>>? EndpointFilterFactories { get; set; }

/// <summary>
/// Gets or sets the <see cref="RoutePattern"/> associated with this endpoint.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Routing/src/RouteEndpointDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public RouteHandlerBuilder AddRouteHandler(
if (isFallback)
{
routeAttributes |= RouteAttributes.Fallback;
}
}

_routeEntries.Add(new()
{
Expand Down Expand Up @@ -196,7 +196,7 @@ private RouteEndpointBuilder CreateRouteEndpointBuilder(
entrySpecificConvention(builder);
}

if (isRouteHandler || builder.EndpointFilterFactories is { Count: > 0})
if (isRouteHandler || builder.FilterFactories.Count > 0)
{
var routeParamNames = new List<string>(pattern.Parameters.Count);
foreach (var parameter in pattern.Parameters)
Expand All @@ -211,7 +211,7 @@ private RouteEndpointBuilder CreateRouteEndpointBuilder(
ThrowOnBadRequest = _throwOnBadRequest,
DisableInferBodyFromParameters = ShouldDisableInferredBodyParameters(entry.HttpMethods),
EndpointMetadata = builder.Metadata,
EndpointFilterFactories = builder.EndpointFilterFactories,
EndpointFilterFactories = builder.FilterFactories.AsReadOnly(),
};

// We ignore the returned EndpointMetadata has been already populated since we passed in non-null EndpointMetadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public async void Build_DoesNot_RunFilters()

var builder = new RouteEndpointBuilder(requestDelegate, RoutePatternFactory.Parse("/"), defaultOrder);

builder.EndpointFilterFactories = new List<Func<EndpointFilterFactoryContext, EndpointFilterDelegate, EndpointFilterDelegate>>();
builder.EndpointFilterFactories.Add((endopintContext, next) =>
builder.FilterFactories.Add((endopintContext, next) =>
{
endpointFilterCallCount++;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Internal;
Expand Down Expand Up @@ -36,6 +37,8 @@ public class ControllerActionDescriptor : ActionDescriptor
/// </summary>
public TypeInfo ControllerTypeInfo { get; set; } = default!;

internal EndpointFilterDelegate? FilterDelegate { get; set; }

// Cache entry so we can avoid an external cache
internal ControllerActionInvokerCacheEntry? CacheEntry { get; set; }

Expand Down
Loading