Skip to content

Add WithOrder to IEndpointConventionBuilder #49093

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 3 commits into from
Aug 9, 2023
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 @@ -126,6 +126,29 @@ public static TBuilder WithGroupName<TBuilder>(this TBuilder builder, string end
return builder;
}

/// <summary>
/// Sets the <see cref="RouteEndpointBuilder.Order"/> to the provided <paramref name="order"/> for all
/// builders created by <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
/// <param name="order">The order assigned to the endpoint.</param>
/// <returns>The <see cref="IEndpointConventionBuilder"/>.</returns>
public static TBuilder WithOrder<TBuilder>(this TBuilder builder, int order) where TBuilder : IEndpointConventionBuilder
{
builder.Add(builder =>
{
if (builder is RouteEndpointBuilder routeEndpointBuilder)
{
routeEndpointBuilder.Order = order;
}
else
{
throw new InvalidOperationException("This endpoint does not support Order.");
}
});
return builder;
}

/// <summary>
/// Disables anti-forgery token validation for all endpoints produced on
/// the target <see cref="IEndpointConventionBuilder"/>.
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 @@ -8,6 +8,7 @@ override Microsoft.AspNetCore.Routing.HttpMethodMetadata.ToString() -> string!
override Microsoft.AspNetCore.Routing.RouteNameMetadata.ToString() -> string!
override Microsoft.AspNetCore.Routing.SuppressLinkGenerationMetadata.ToString() -> string!
override Microsoft.AspNetCore.Routing.SuppressMatchingMetadata.ToString() -> string!
static Microsoft.AspNetCore.Builder.RoutingEndpointConventionBuilderExtensions.WithOrder<TBuilder>(this TBuilder builder, int order) -> TBuilder
Microsoft.AspNetCore.Builder.RouteShortCircuitEndpointConventionBuilderExtensions
Microsoft.AspNetCore.Routing.RouteShortCircuitEndpointRouteBuilderExtensions
static Microsoft.AspNetCore.Builder.RouteShortCircuitEndpointConventionBuilderExtensions.ShortCircuit(this Microsoft.AspNetCore.Builder.IEndpointConventionBuilder! builder, int? statusCode = null) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ public void FinallyConventions_CanExamineMetadataInFIFOOrder()
Assert.Equal(new[] { "test-metadata", "inner-metadata", "inner-metadata-2" }, metadata);
}

[Fact]
public void WithOrder_Func_SetsOrder()
{
// Arrange
var builder = CreateBuilder();

// Act
builder.WithOrder(47);

// Assert
var endpoint = builder.Build() as RouteEndpoint;
Assert.Equal(47, endpoint.Order);
}

private TestEndpointConventionBuilder CreateBuilder()
{
var conventionBuilder = new DefaultEndpointConventionBuilder(new RouteEndpointBuilder(
Expand Down