From 7c16c92317d6cc815630e201ef47b4a73e6b4006 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Mon, 24 Sep 2018 20:01:36 -0700 Subject: [PATCH] Fix a bug in GetUriByRouteValues Pride cometh before the fall... --- ...nkGeneratorRouteValuesAddressExtensions.cs | 2 +- ...neratorRouteValuesAddressExtensionsTest.cs | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Routing/LinkGeneratorRouteValuesAddressExtensions.cs b/src/Microsoft.AspNetCore.Routing/LinkGeneratorRouteValuesAddressExtensions.cs index a2bffc01ef..d025679e41 100644 --- a/src/Microsoft.AspNetCore.Routing/LinkGeneratorRouteValuesAddressExtensions.cs +++ b/src/Microsoft.AspNetCore.Routing/LinkGeneratorRouteValuesAddressExtensions.cs @@ -135,7 +135,7 @@ namespace Microsoft.AspNetCore.Routing throw new ArgumentNullException(nameof(httpContext)); } - var address = CreateAddress(httpContext: null, routeName, values); + var address = CreateAddress(httpContext, routeName, values); return generator.GetUriByAddress( httpContext, address, diff --git a/test/Microsoft.AspNetCore.Routing.Tests/LinkGeneratorRouteValuesAddressExtensionsTest.cs b/test/Microsoft.AspNetCore.Routing.Tests/LinkGeneratorRouteValuesAddressExtensionsTest.cs index 57874816d6..57f530cdb8 100644 --- a/test/Microsoft.AspNetCore.Routing.Tests/LinkGeneratorRouteValuesAddressExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Routing.Tests/LinkGeneratorRouteValuesAddressExtensionsTest.cs @@ -170,6 +170,38 @@ namespace Microsoft.AspNetCore.Routing Assert.Equal("http://example.com/Foo/Bar%3Fencodeme%3F/Home/Index/?query=some%3Fquery#Fragment?", uri); } + [Fact] + public void GetUriByRouteValues_WithHttpContext_CanUseAmbientValues() + { + // Arrange + var endpoint1 = EndpointFactory.CreateRouteEndpoint( + "Home/Index/{id}", + defaults: new { controller = "Home", action = "Index", }, + metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) }); + var endpoint2 = EndpointFactory.CreateRouteEndpoint( + "Home/Index/{id?}", + defaults: new { controller = "Home", action = "Index", }, + metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) }); + + var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2); + + var httpContext = CreateHttpContext(new { controller = "Home", }); + httpContext.Request.Scheme = "http"; + httpContext.Request.Host = new HostString("example.com"); + httpContext.Request.PathBase = new PathString("/Foo/Bar?encodeme?"); + + // Act + var uri = linkGenerator.GetUriByRouteValues( + httpContext, + routeName: null, + values: new RouteValueDictionary(new { action = "Index", query = "some?query" }), + fragment: new FragmentString("#Fragment?"), + options: new LinkOptions() { AppendTrailingSlash = true, }); + + // Assert + Assert.Equal("http://example.com/Foo/Bar%3Fencodeme%3F/Home/Index/?query=some%3Fquery#Fragment?", uri); + } + [Fact] public void GetTemplateByRouteValues_CreatesTemplate() {