diff --git a/src/Http/Routing/src/DefaultLinkGenerator.cs b/src/Http/Routing/src/DefaultLinkGenerator.cs index 086d9bf026..9b9f040cd2 100644 --- a/src/Http/Routing/src/DefaultLinkGenerator.cs +++ b/src/Http/Routing/src/DefaultLinkGenerator.cs @@ -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 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, diff --git a/src/Http/Routing/test/UnitTests/DefaultLinkGeneratorTest.cs b/src/Http/Routing/test/UnitTests/DefaultLinkGeneratorTest.cs index 3c20c704a0..6d0c9204b3 100644 --- a/src/Http/Routing/test/UnitTests/DefaultLinkGeneratorTest.cs +++ b/src/Http/Routing/test/UnitTests/DefaultLinkGeneratorTest.cs @@ -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() {