Fix HttpContext not being passed to constraints in link generation (#6644)

This commit is contained in:
James Newton-King 2019-01-16 12:00:57 +13:00 committed by GitHub
parent 92680b355f
commit cd308e7a8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 4 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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.
using System;
@ -90,6 +90,7 @@ namespace Microsoft.AspNetCore.Routing
}
return GetPathByEndpoints(
httpContext,
endpoints,
values,
ambientValues,
@ -112,6 +113,7 @@ namespace Microsoft.AspNetCore.Routing
}
return GetPathByEndpoints(
httpContext: null,
endpoints,
values,
ambientValues: null,
@ -206,7 +208,8 @@ namespace Microsoft.AspNetCore.Routing
return endpoints;
}
public string GetPathByEndpoints(
private string GetPathByEndpoints(
HttpContext httpContext,
List<RouteEndpoint> endpoints,
RouteValueDictionary values,
RouteValueDictionary ambientValues,
@ -218,7 +221,7 @@ namespace Microsoft.AspNetCore.Routing
{
var endpoint = endpoints[i];
if (TryProcessTemplate(
httpContext: null,
httpContext: httpContext,
endpoint: endpoint,
values: values,
ambientValues: ambientValues,

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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.
using System;
@ -524,6 +524,42 @@ namespace Microsoft.AspNetCore.Routing
Assert.Equal("ftp://example.com:5000/Home/Index", uri);
}
[Fact]
public void GetPathByAddress_WithHttpContext_ContextPassedToConstraint()
{
// Arrange
var constraint = new TestRouteConstraint();
var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id?}", policies: new { controller = constraint }, metadata: new object[] { new IntMetadata(1), });
var linkGenerator = CreateLinkGenerator(endpoint1);
var httpContext = CreateHttpContext();
httpContext.Request.PathBase = "/Foo";
// Act
var uri = linkGenerator.GetPathByAddress(
httpContext,
1,
values: new RouteValueDictionary(new { action = "Index", controller = "Home", }),
pathBase: "/");
// Assert
Assert.Equal("/Home/Index", uri);
Assert.True(constraint.HasHttpContext);
}
private class TestRouteConstraint : IRouteConstraint
{
public bool HasHttpContext { get; set; }
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
{
HasHttpContext = (httpContext != null);
return true;
}
}
[Fact]
public void GetTemplateBinder_CanCache()
{