Support supression of matching for endpoints

This commit is contained in:
Ryan Nowak 2018-08-15 20:57:40 -07:00
parent 085a0b808e
commit 97f54c532b
5 changed files with 49 additions and 2 deletions

View File

@ -0,0 +1,13 @@
// 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
{
/// <summary>
/// Metadata used to prevent URL matching. The associated endpoint will not be
/// considered URL matching for incoming requests.
/// </summary>
public interface ISuppressMatchingMetadata
{
}
}

View File

@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.Routing.Internal
for (var i = 0; i < endpoints.Count; i++)
{
var endpoint = endpoints[i] as MatcherEndpoint;
if (endpoint != null)
if (endpoint != null && endpoint.Metadata.GetMetadata<ISuppressMatchingMetadata>() == null)
{
builder.AddEndpoint(endpoint);
}

View File

@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Routing.Matching
// register other endpoint types, which are non-routable, and it's
// ok that we won't route to them.
var endpoint = endpoints[i] as MatcherEndpoint;
if (endpoint != null)
if (endpoint != null && endpoint.Metadata.GetMetadata<ISuppressMatchingMetadata>() == null)
{
builder.AddEndpoint(endpoint);
}

View File

@ -0,0 +1,13 @@
// 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
{
/// <summary>
/// Metadata used to prevent URL matching. The associated endpoint will not be
/// considered URL matching for incoming requests.
/// </summary>
public sealed class SuppressMatchingMetadata : ISuppressMatchingMetadata
{
}
}

View File

@ -67,6 +67,27 @@ namespace Microsoft.AspNetCore.Routing.Matching
Assert.Empty(inner.Endpoints);
}
[Fact]
public void Matcher_Ignores_SuppressedEndpoint()
{
// Arrange
var dataSource = new DynamicEndpointDataSource();
var endpoint = new MatcherEndpoint(
MatcherEndpoint.EmptyInvoker,
RoutePatternFactory.Parse("/"),
0,
new EndpointMetadataCollection(new SuppressMatchingMetadata()),
"test");
dataSource.AddEndpoint(endpoint);
// Act
var matcher = new DataSourceDependentMatcher(dataSource, TestMatcherBuilder.Create);
// Assert
var inner = Assert.IsType<TestMatcher>(matcher.CurrentMatcher);
Assert.Empty(inner.Endpoints);
}
[Fact]
public void Cache_Reinitializes_WhenDataSourceChanges()
{