From dbf1dca723fbca2235396c9a7a7e4bbc71e9b32b Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 21 Dec 2018 10:19:03 +1300 Subject: [PATCH] Fix HttpContext not being passed to constraints in link generation (#6045) --- src/Http/Routing/src/DefaultLinkGenerator.cs | 9 +++-- .../UnitTests/DefaultLinkGeneratorTest.cs | 38 ++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/Http/Routing/src/DefaultLinkGenerator.cs b/src/Http/Routing/src/DefaultLinkGenerator.cs index 64c59857c6..7670b7c6b4 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 120c4995b1..ef05217153 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; @@ -529,6 +529,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() {