Merge branch 'release/2.2'
This commit is contained in:
commit
cfcedff233
|
|
@ -15,6 +15,11 @@ namespace Microsoft.AspNetCore.Routing.Internal
|
||||||
[DebuggerDisplay("{DebuggerDisplayString,nq}")]
|
[DebuggerDisplay("{DebuggerDisplayString,nq}")]
|
||||||
public class LinkGenerationDecisionTree
|
public class LinkGenerationDecisionTree
|
||||||
{
|
{
|
||||||
|
// Fallback value for cases where the ambient values weren't provided.
|
||||||
|
//
|
||||||
|
// This is safe because we don't mutate the route values in here.
|
||||||
|
private static readonly RouteValueDictionary EmptyAmbientValues = new RouteValueDictionary();
|
||||||
|
|
||||||
private readonly DecisionTreeNode<OutboundMatch> _root;
|
private readonly DecisionTreeNode<OutboundMatch> _root;
|
||||||
|
|
||||||
public LinkGenerationDecisionTree(IReadOnlyList<OutboundMatch> entries)
|
public LinkGenerationDecisionTree(IReadOnlyList<OutboundMatch> entries)
|
||||||
|
|
@ -30,7 +35,7 @@ namespace Microsoft.AspNetCore.Routing.Internal
|
||||||
if (_root.Matches.Count > 0 || _root.Criteria.Count > 0)
|
if (_root.Matches.Count > 0 || _root.Criteria.Count > 0)
|
||||||
{
|
{
|
||||||
var results = new List<OutboundMatchResult>();
|
var results = new List<OutboundMatchResult>();
|
||||||
Walk(results, values, ambientValues, _root, isFallbackPath: false);
|
Walk(results, values, ambientValues ?? EmptyAmbientValues, _root, isFallbackPath: false);
|
||||||
results.Sort(OutboundMatchResultComparer.Instance);
|
results.Sort(OutboundMatchResultComparer.Instance);
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -135,7 +135,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
throw new ArgumentNullException(nameof(httpContext));
|
throw new ArgumentNullException(nameof(httpContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
var address = CreateAddress(httpContext: null, routeName, values);
|
var address = CreateAddress(httpContext, routeName, values);
|
||||||
return generator.GetUriByAddress<RouteValuesAddress>(
|
return generator.GetUriByAddress<RouteValuesAddress>(
|
||||||
httpContext,
|
httpContext,
|
||||||
address,
|
address,
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,26 @@ namespace Microsoft.AspNetCore.Routing.Internal.Routing
|
||||||
{
|
{
|
||||||
public class LinkGenerationDecisionTreeTest
|
public class LinkGenerationDecisionTreeTest
|
||||||
{
|
{
|
||||||
|
[Fact]
|
||||||
|
public void GetMatches_AllowsNullAmbientValues()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var entries = new List<OutboundMatch>();
|
||||||
|
|
||||||
|
var entry = CreateMatch(new { });
|
||||||
|
entries.Add(entry);
|
||||||
|
|
||||||
|
var tree = new LinkGenerationDecisionTree(entries);
|
||||||
|
|
||||||
|
var context = CreateContext(new { });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var matches = tree.GetMatches(context.Values, ambientValues: null);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Same(entry, Assert.Single(matches).Match);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void SelectSingleEntry_NoCriteria()
|
public void SelectSingleEntry_NoCriteria()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -170,6 +170,38 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
Assert.Equal("http://example.com/Foo/Bar%3Fencodeme%3F/Home/Index/?query=some%3Fquery#Fragment?", uri);
|
Assert.Equal("http://example.com/Foo/Bar%3Fencodeme%3F/Home/Index/?query=some%3Fquery#Fragment?", uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetUriByRouteValues_WithHttpContext_CanUseAmbientValues()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var endpoint1 = EndpointFactory.CreateRouteEndpoint(
|
||||||
|
"Home/Index/{id}",
|
||||||
|
defaults: new { controller = "Home", action = "Index", },
|
||||||
|
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||||
|
var endpoint2 = EndpointFactory.CreateRouteEndpoint(
|
||||||
|
"Home/Index/{id?}",
|
||||||
|
defaults: new { controller = "Home", action = "Index", },
|
||||||
|
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||||
|
|
||||||
|
var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);
|
||||||
|
|
||||||
|
var httpContext = CreateHttpContext(new { controller = "Home", });
|
||||||
|
httpContext.Request.Scheme = "http";
|
||||||
|
httpContext.Request.Host = new HostString("example.com");
|
||||||
|
httpContext.Request.PathBase = new PathString("/Foo/Bar?encodeme?");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var uri = linkGenerator.GetUriByRouteValues(
|
||||||
|
httpContext,
|
||||||
|
routeName: null,
|
||||||
|
values: new RouteValueDictionary(new { action = "Index", query = "some?query" }),
|
||||||
|
fragment: new FragmentString("#Fragment?"),
|
||||||
|
options: new LinkOptions() { AppendTrailingSlash = true, });
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("http://example.com/Foo/Bar%3Fencodeme%3F/Home/Index/?query=some%3Fquery#Fragment?", uri);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void GetTemplateByRouteValues_CreatesTemplate()
|
public void GetTemplateByRouteValues_CreatesTemplate()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue