Updated MvcEndpointDataSource to create endpoints with SuppressLinkGenerationMetadata

This commit is contained in:
Kiran Challa 2018-07-24 14:59:51 -07:00 committed by Kiran Challa
parent bcd6e83591
commit 10ce77b9ca
2 changed files with 41 additions and 6 deletions

View File

@ -120,7 +120,8 @@ namespace Microsoft.AspNetCore.Mvc.Internal
subTemplate, subTemplate,
endpointInfo.Defaults, endpointInfo.Defaults,
++conventionalRouteOrder, ++conventionalRouteOrder,
endpointInfo); endpointInfo,
suppressLinkGeneration: false);
endpoints.Add(subEndpoint); endpoints.Add(subEndpoint);
} }
@ -145,7 +146,8 @@ namespace Microsoft.AspNetCore.Mvc.Internal
newTemplate, newTemplate,
endpointInfo.Defaults, endpointInfo.Defaults,
++conventionalRouteOrder, ++conventionalRouteOrder,
endpointInfo); endpointInfo,
suppressLinkGeneration: false);
endpoints.Add(endpoint); endpoints.Add(endpoint);
} }
} }
@ -158,7 +160,8 @@ namespace Microsoft.AspNetCore.Mvc.Internal
action.AttributeRouteInfo.Template, action.AttributeRouteInfo.Template,
nonInlineDefaults: null, nonInlineDefaults: null,
action.AttributeRouteInfo.Order, action.AttributeRouteInfo.Order,
action.AttributeRouteInfo); action.AttributeRouteInfo,
suppressLinkGeneration: action.AttributeRouteInfo.SuppressLinkGeneration);
endpoints.Add(endpoint); endpoints.Add(endpoint);
} }
} }
@ -266,7 +269,8 @@ namespace Microsoft.AspNetCore.Mvc.Internal
string template, string template,
object nonInlineDefaults, object nonInlineDefaults,
int order, int order,
object source) object source,
bool suppressLinkGeneration)
{ {
RequestDelegate invokerDelegate = (context) => RequestDelegate invokerDelegate = (context) =>
{ {
@ -289,7 +293,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
var defaults = new RouteValueDictionary(nonInlineDefaults); var defaults = new RouteValueDictionary(nonInlineDefaults);
EnsureRequiredValuesInDefaults(action.RouteValues, defaults); EnsureRequiredValuesInDefaults(action.RouteValues, defaults);
var metadataCollection = BuildEndpointMetadata(action, routeName, source); var metadataCollection = BuildEndpointMetadata(action, routeName, source, suppressLinkGeneration);
var endpoint = new MatcherEndpoint( var endpoint = new MatcherEndpoint(
next => invokerDelegate, next => invokerDelegate,
RoutePatternFactory.Parse(template, defaults, constraints: null), RoutePatternFactory.Parse(template, defaults, constraints: null),
@ -301,7 +305,11 @@ namespace Microsoft.AspNetCore.Mvc.Internal
return endpoint; return endpoint;
} }
private static EndpointMetadataCollection BuildEndpointMetadata(ActionDescriptor action, string routeName, object source) private static EndpointMetadataCollection BuildEndpointMetadata(
ActionDescriptor action,
string routeName,
object source,
bool suppressLinkGeneration)
{ {
var metadata = new List<object>(); var metadata = new List<object>();
// REVIEW: Used for debugging. Consider removing before release // REVIEW: Used for debugging. Consider removing before release
@ -341,6 +349,11 @@ namespace Microsoft.AspNetCore.Mvc.Internal
} }
} }
if (suppressLinkGeneration)
{
metadata.Add(new SuppressLinkGenerationMetadata());
}
var metadataCollection = new EndpointMetadataCollection(metadata); var metadataCollection = new EndpointMetadataCollection(metadata);
return metadataCollection; return metadataCollection;
} }
@ -428,5 +441,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
public string Name { get; } public string Name { get; }
} }
private class SuppressLinkGenerationMetadata : ISuppressLinkGenerationMetadata { }
} }
} }

View File

@ -93,6 +93,24 @@ namespace Microsoft.AspNetCore.Mvc.Routing
Assert.Equal("/api/orders/10", url); Assert.Equal("/api/orders/10", url);
} }
[Fact]
public void RouteUrl_DoesNotGenerateLink_ToEndpointsWithSuppressLinkGeneration()
{
// Arrange
var endpoint = CreateEndpoint(
"Home/Index",
defaults: new { controller = "Home", action = "Index" },
requiredValues: new { controller = "Home", action = "Index" },
metadataCollection: new EndpointMetadataCollection(new[] { new SuppressLinkGenerationMetadata() }));
var urlHelper = CreateUrlHelper(new[] { endpoint });
// Act
var url = urlHelper.RouteUrl(new { controller = "Home", action = "Index" });
// Assert
Assert.Null(url);
}
protected override IUrlHelper CreateUrlHelper(string appRoot, string host, string protocol) protected override IUrlHelper CreateUrlHelper(string appRoot, string host, string protocol)
{ {
return CreateUrlHelper(Enumerable.Empty<MatcherEndpoint>(), appRoot, host, protocol); return CreateUrlHelper(Enumerable.Empty<MatcherEndpoint>(), appRoot, host, protocol);
@ -313,5 +331,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing
public string Name { get; } public string Name { get; }
} }
private class SuppressLinkGenerationMetadata : ISuppressLinkGenerationMetadata { }
} }
} }