Skip to content

[Mvc] Add support for order in dynamic controller routes #25073

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 2 commits into from
Aug 25, 2020
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
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -506,18 +506,10 @@ public static void MapDynamicControllerRoute<TTransformer>(this IEndpointRouteBu
EnsureControllerServices(endpoints);

// Called for side-effect to make sure that the data source is registered.
GetOrCreateDataSource(endpoints).CreateInertEndpoints = true;

endpoints.Map(
pattern,
context =>
{
throw new InvalidOperationException("This endpoint is not expected to be executed directly.");
})
.Add(b =>
{
b.Metadata.Add(new DynamicControllerRouteValueTransformerMetadata(typeof(TTransformer), state));
});
var controllerDataSource = GetOrCreateDataSource(endpoints);

// The data source is just used to share the common order with conventionally routed actions.
controllerDataSource.AddDynamicControllerEndpoint(endpoints, pattern, typeof(TTransformer), state);
}

private static DynamicControllerMetadata CreateDynamicControllerMetadata(string action, string controller, string area)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ internal static void AddMvcCoreServices(IServiceCollection services)
//
// Endpoint Routing / Endpoints
//
services.TryAddSingleton<OrderedEndpointsSequenceProvider>();
services.TryAddSingleton<ControllerActionEndpointDataSource>();
services.TryAddSingleton<ActionEndpointFactory>();
services.TryAddSingleton<DynamicControllerEndpointSelector>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.AspNetCore.Mvc.Infrastructure
{
internal class OrderedEndpointsSequenceProvider
{
private object Lock = new object();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly


// In traditional conventional routing setup, the routes defined by a user have a order
// defined by how they are added into the list. We would like to maintain the same order when building
// up the endpoints too.
//
// Start with an order of '1' for conventional routes as attribute routes have a default order of '0'.
// This is for scenarios dealing with migrating existing Router based code to Endpoint Routing world.
private int _current = 1;

public int GetNext()
{
lock (Lock)
{
return _current++;
}
}
}
}
41 changes: 27 additions & 14 deletions src/Mvc/Mvc.Core/src/Routing/ControllerActionEndpointDataSource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand All @@ -15,27 +15,19 @@ namespace Microsoft.AspNetCore.Mvc.Routing
internal class ControllerActionEndpointDataSource : ActionEndpointDataSourceBase
{
private readonly ActionEndpointFactory _endpointFactory;
private readonly OrderedEndpointsSequenceProvider _orderSequence;
private readonly List<ConventionalRouteEntry> _routes;

private int _order;

public ControllerActionEndpointDataSource(
IActionDescriptorCollectionProvider actions,
ActionEndpointFactory endpointFactory)
ActionEndpointFactory endpointFactory,
OrderedEndpointsSequenceProvider orderSequence)
: base(actions)
{
_endpointFactory = endpointFactory;

_orderSequence = orderSequence;
_routes = new List<ConventionalRouteEntry>();

// In traditional conventional routing setup, the routes defined by a user have a order
// defined by how they are added into the list. We would like to maintain the same order when building
// up the endpoints too.
//
// Start with an order of '1' for conventional routes as attribute routes have a default order of '0'.
// This is for scenarios dealing with migrating existing Router based code to Endpoint Routing world.
_order = 1;

DefaultBuilder = new ControllerActionEndpointConventionBuilder(Lock, Conventions);

// IMPORTANT: this needs to be the last thing we do in the constructor.
Expand All @@ -59,7 +51,7 @@ public ControllerActionEndpointConventionBuilder AddRoute(
lock (Lock)
{
var conventions = new List<Action<EndpointBuilder>>();
_routes.Add(new ConventionalRouteEntry(routeName, pattern, defaults, constraints, dataTokens, _order++, conventions));
_routes.Add(new ConventionalRouteEntry(routeName, pattern, defaults, constraints, dataTokens, _orderSequence.GetNext(), conventions));
return new ControllerActionEndpointConventionBuilder(Lock, conventions);
}
}
Expand Down Expand Up @@ -108,6 +100,27 @@ protected override List<Endpoint> CreateEndpoints(IReadOnlyList<ActionDescriptor

return endpoints;
}

internal void AddDynamicControllerEndpoint(IEndpointRouteBuilder endpoints, string pattern, Type transformerType, object state)
{
CreateInertEndpoints = true;
lock (Lock)
{
var order = _orderSequence.GetNext();

endpoints.Map(
pattern,
context =>
{
throw new InvalidOperationException("This endpoint is not expected to be executed directly.");
})
.Add(b =>
{
((RouteEndpointBuilder)b).Order = order;
b.Metadata.Add(new DynamicControllerRouteValueTransformerMetadata(transformerType, state));
});
}
}
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -385,7 +385,7 @@ private static bool SupportsLinkGeneration(RouteEndpoint endpoint)

private protected override ActionEndpointDataSourceBase CreateDataSource(IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory)
{
return new ControllerActionEndpointDataSource(actions, endpointFactory);
return new ControllerActionEndpointDataSource(actions, endpointFactory, new OrderedEndpointsSequenceProvider());
}

protected override ActionDescriptor CreateActionDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,18 +337,9 @@ public static void MapDynamicPageRoute<TTransformer>(this IEndpointRouteBuilder
EnsureRazorPagesServices(endpoints);

// Called for side-effect to make sure that the data source is registered.
GetOrCreateDataSource(endpoints).CreateInertEndpoints = true;
var dataSource = GetOrCreateDataSource(endpoints);

endpoints.Map(
pattern,
context =>
{
throw new InvalidOperationException("This endpoint is not expected to be executed directly.");
})
.Add(b =>
{
b.Metadata.Add(new DynamicPageRouteValueTransformerMetadata(typeof(TTransformer), state));
});
dataSource.AddDynamicPageEndpoint(endpoints, pattern, typeof(TTransformer), state);
}

private static DynamicPageMetadata CreateDynamicPageMetadata(string page, string area)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand All @@ -8,18 +8,23 @@
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;

namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
internal class PageActionEndpointDataSource : ActionEndpointDataSourceBase
{
private readonly ActionEndpointFactory _endpointFactory;
private readonly OrderedEndpointsSequenceProvider _orderSequence;

public PageActionEndpointDataSource(IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory)
public PageActionEndpointDataSource(
IActionDescriptorCollectionProvider actions,
ActionEndpointFactory endpointFactory,
OrderedEndpointsSequenceProvider orderedEndpoints)
: base(actions)
{
_endpointFactory = endpointFactory;

_orderSequence = orderedEndpoints;
DefaultBuilder = new PageActionEndpointConventionBuilder(Lock, Conventions);

// IMPORTANT: this needs to be the last thing we do in the constructor.
Expand Down Expand Up @@ -47,6 +52,27 @@ protected override List<Endpoint> CreateEndpoints(IReadOnlyList<ActionDescriptor

return endpoints;
}

internal void AddDynamicPageEndpoint(IEndpointRouteBuilder endpoints, string pattern, Type transformerType, object state)
{
CreateInertEndpoints = true;
lock (Lock)
{
var order = _orderSequence.GetNext();

endpoints.Map(
pattern,
context =>
{
throw new InvalidOperationException("This endpoint is not expected to be executed directly.");
})
.Add(b =>
{
((RouteEndpointBuilder)b).Order = order;
b.Metadata.Add(new DynamicPageRouteValueTransformerMetadata(transformerType, state));
});
}
}
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -92,7 +92,7 @@ public void Endpoints_AppliesConventions()

private protected override ActionEndpointDataSourceBase CreateDataSource(IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory)
{
return new PageActionEndpointDataSource(actions, endpointFactory);
return new PageActionEndpointDataSource(actions, endpointFactory, new OrderedEndpointsSequenceProvider());
}

protected override ActionDescriptor CreateActionDescriptor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -110,7 +110,8 @@ private ControllerActionEndpointDataSource CreateDataSource(IActionDescriptorCol
{
var dataSource = new ControllerActionEndpointDataSource(
actionDescriptorCollectionProvider,
new ActionEndpointFactory(new MockRoutePatternTransformer()));
new ActionEndpointFactory(new MockRoutePatternTransformer()),
new OrderedEndpointsSequenceProvider());

return dataSource;
}
Expand Down
Loading