From 34499dbe24034e402a483a9dd70722edab3a615f Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Tue, 24 Jul 2018 12:19:16 -0700 Subject: [PATCH] Added support for suppressing link generation for endpoints --- .../ISuppressLinkGenerationMetadata.cs | 9 +++++++++ .../RouteValuesBasedEndpointFinder.cs | 7 +++++++ .../RouteValueBasedEndpointFinderTest.cs | 17 +++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 src/Microsoft.AspNetCore.Routing.Abstractions/ISuppressLinkGenerationMetadata.cs diff --git a/src/Microsoft.AspNetCore.Routing.Abstractions/ISuppressLinkGenerationMetadata.cs b/src/Microsoft.AspNetCore.Routing.Abstractions/ISuppressLinkGenerationMetadata.cs new file mode 100644 index 0000000000..2e6a7da5d8 --- /dev/null +++ b/src/Microsoft.AspNetCore.Routing.Abstractions/ISuppressLinkGenerationMetadata.cs @@ -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 + { + } +} diff --git a/src/Microsoft.AspNetCore.Routing/RouteValuesBasedEndpointFinder.cs b/src/Microsoft.AspNetCore.Routing/RouteValuesBasedEndpointFinder.cs index 2c3afaf55d..e4fc5c16a7 100644 --- a/src/Microsoft.AspNetCore.Routing/RouteValuesBasedEndpointFinder.cs +++ b/src/Microsoft.AspNetCore.Routing/RouteValuesBasedEndpointFinder.cs @@ -107,6 +107,13 @@ namespace Microsoft.AspNetCore.Routing var endpoints = _endpointDataSource.Endpoints.OfType(); 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(); + if (suppressLinkGeneration != null) + { + continue; + } + var entry = CreateOutboundRouteEntry(endpoint); var outboundMatch = new OutboundMatch() { Entry = entry }; diff --git a/test/Microsoft.AspNetCore.Routing.Tests/RouteValueBasedEndpointFinderTest.cs b/test/Microsoft.AspNetCore.Routing.Tests/RouteValueBasedEndpointFinderTest.cs index 6296604d2a..fab9c41600 100644 --- a/test/Microsoft.AspNetCore.Routing.Tests/RouteValueBasedEndpointFinderTest.cs +++ b/test/Microsoft.AspNetCore.Routing.Tests/RouteValueBasedEndpointFinderTest.cs @@ -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 { } } }