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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -90,6 +90,7 @@ namespace Microsoft.AspNetCore.Routing
} }
return GetPathByEndpoints( return GetPathByEndpoints(
httpContext,
endpoints, endpoints,
values, values,
ambientValues, ambientValues,
@ -112,6 +113,7 @@ namespace Microsoft.AspNetCore.Routing
} }
return GetPathByEndpoints( return GetPathByEndpoints(
httpContext: null,
endpoints, endpoints,
values, values,
ambientValues: null, ambientValues: null,
@ -206,7 +208,8 @@ namespace Microsoft.AspNetCore.Routing
return endpoints; return endpoints;
} }
public string GetPathByEndpoints( private string GetPathByEndpoints(
HttpContext httpContext,
List<RouteEndpoint> endpoints, List<RouteEndpoint> endpoints,
RouteValueDictionary values, RouteValueDictionary values,
RouteValueDictionary ambientValues, RouteValueDictionary ambientValues,
@ -218,7 +221,7 @@ namespace Microsoft.AspNetCore.Routing
{ {
var endpoint = endpoints[i]; var endpoint = endpoints[i];
if (TryProcessTemplate( if (TryProcessTemplate(
httpContext: null, httpContext: httpContext,
endpoint: endpoint, endpoint: endpoint,
values: values, values: values,
ambientValues: ambientValues, 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -524,6 +524,42 @@ namespace Microsoft.AspNetCore.Routing
Assert.Equal("ftp://example.com:5000/Home/Index", uri); 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] [Fact]
public void GetTemplateBinder_CanCache() public void GetTemplateBinder_CanCache()
{ {