Added support for suppressing link generation for endpoints
This commit is contained in:
parent
deddb336b7
commit
34499dbe24
|
|
@ -0,0 +1,9 @@
|
|||
// 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.Routing
|
||||
{
|
||||
public interface ISuppressLinkGenerationMetadata
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -107,6 +107,13 @@ namespace Microsoft.AspNetCore.Routing
|
|||
var endpoints = _endpointDataSource.Endpoints.OfType<MatcherEndpoint>();
|
||||
foreach (var endpoint in endpoints)
|
||||
{
|
||||
// Do not consider an endpoint for link generation if the following marker metadata is on it
|
||||
var suppressLinkGeneration = endpoint.Metadata.GetMetadata<ISuppressLinkGenerationMetadata>();
|
||||
if (suppressLinkGeneration != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var entry = CreateOutboundRouteEntry(endpoint);
|
||||
|
||||
var outboundMatch = new OutboundMatch() { Entry = entry };
|
||||
|
|
|
|||
|
|
@ -224,6 +224,21 @@ namespace Microsoft.AspNetCore.Routing
|
|||
Assert.Same(expected, actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetOutboundMatches_DoesNotInclude_EndpointsWithSuppressLinkGenerationMetadata()
|
||||
{
|
||||
// Arrange
|
||||
var endpoint = CreateEndpoint(
|
||||
"/a",
|
||||
metadataCollection: new EndpointMetadataCollection(new[] { new SuppressLinkGenerationMetadata() }));
|
||||
|
||||
// Act
|
||||
var finder = CreateEndpointFinder(endpoint);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(finder.AllMatches);
|
||||
}
|
||||
|
||||
private CustomRouteValuesBasedEndpointFinder CreateEndpointFinder(params Endpoint[] endpoints)
|
||||
{
|
||||
return CreateEndpointFinder(new DefaultEndpointDataSource(endpoints));
|
||||
|
|
@ -304,5 +319,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
return matches;
|
||||
}
|
||||
}
|
||||
|
||||
private class SuppressLinkGenerationMetadata : ISuppressLinkGenerationMetadata { }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue