Skip to content

Commit cd308e7

Browse files
authored
Fix HttpContext not being passed to constraints in link generation (#6644)
1 parent 92680b3 commit cd308e7

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/Http/Routing/src/DefaultLinkGenerator.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -90,6 +90,7 @@ public override string GetPathByAddress<TAddress>(
9090
}
9191

9292
return GetPathByEndpoints(
93+
httpContext,
9394
endpoints,
9495
values,
9596
ambientValues,
@@ -112,6 +113,7 @@ public override string GetPathByAddress<TAddress>(
112113
}
113114

114115
return GetPathByEndpoints(
116+
httpContext: null,
115117
endpoints,
116118
values,
117119
ambientValues: null,
@@ -206,7 +208,8 @@ private List<RouteEndpoint> GetEndpoints<TAddress>(TAddress address)
206208
return endpoints;
207209
}
208210

209-
public string GetPathByEndpoints(
211+
private string GetPathByEndpoints(
212+
HttpContext httpContext,
210213
List<RouteEndpoint> endpoints,
211214
RouteValueDictionary values,
212215
RouteValueDictionary ambientValues,
@@ -218,7 +221,7 @@ public string GetPathByEndpoints(
218221
{
219222
var endpoint = endpoints[i];
220223
if (TryProcessTemplate(
221-
httpContext: null,
224+
httpContext: httpContext,
222225
endpoint: endpoint,
223226
values: values,
224227
ambientValues: ambientValues,

src/Http/Routing/test/UnitTests/DefaultLinkGeneratorTest.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -524,6 +524,42 @@ public void GetUriByAddress_WithHttpContext_CanOverrideUriParts()
524524
Assert.Equal("ftp://example.com:5000/Home/Index", uri);
525525
}
526526

527+
[Fact]
528+
public void GetPathByAddress_WithHttpContext_ContextPassedToConstraint()
529+
{
530+
// Arrange
531+
var constraint = new TestRouteConstraint();
532+
533+
var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id?}", policies: new { controller = constraint }, metadata: new object[] { new IntMetadata(1), });
534+
535+
var linkGenerator = CreateLinkGenerator(endpoint1);
536+
537+
var httpContext = CreateHttpContext();
538+
httpContext.Request.PathBase = "/Foo";
539+
540+
// Act
541+
var uri = linkGenerator.GetPathByAddress(
542+
httpContext,
543+
1,
544+
values: new RouteValueDictionary(new { action = "Index", controller = "Home", }),
545+
pathBase: "/");
546+
547+
// Assert
548+
Assert.Equal("/Home/Index", uri);
549+
Assert.True(constraint.HasHttpContext);
550+
}
551+
552+
private class TestRouteConstraint : IRouteConstraint
553+
{
554+
public bool HasHttpContext { get; set; }
555+
556+
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
557+
{
558+
HasHttpContext = (httpContext != null);
559+
return true;
560+
}
561+
}
562+
527563
[Fact]
528564
public void GetTemplateBinder_CanCache()
529565
{

0 commit comments

Comments
 (0)