Fix HttpContext not being passed to constraints in link generation (#6644)
This commit is contained in:
parent
92680b355f
commit
cd308e7a8b
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue