From 65c78d12b5382f9f44a764bc7266e17af1ab6af4 Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Tue, 30 May 2017 20:42:28 -0400 Subject: [PATCH] Preserve redirect query string (#237) --- .../Internal/RedirectRule.cs | 2 +- .../MiddlewareTests.cs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectRule.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectRule.cs index 4f63bf6..1c5efaa 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectRule.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/RedirectRule.cs @@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal } else { - response.Headers[HeaderNames.Location] = pathBase + newPath; + response.Headers[HeaderNames.Location] = pathBase + newPath + context.HttpContext.Request.QueryString; } context.Logger?.RedirectedSummary(newPath); diff --git a/test/Microsoft.AspNetCore.Rewrite.Tests/MiddlewareTests.cs b/test/Microsoft.AspNetCore.Rewrite.Tests/MiddlewareTests.cs index 137448b..682c9c0 100644 --- a/test/Microsoft.AspNetCore.Rewrite.Tests/MiddlewareTests.cs +++ b/test/Microsoft.AspNetCore.Rewrite.Tests/MiddlewareTests.cs @@ -51,6 +51,22 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.CodeRules Assert.Equal("http://example.com/foo", response.Headers.Location.OriginalString); } + [Fact] + public async Task CheckRedirectPathWithQueryString() + { + var options = new RewriteOptions().AddRedirect("(.*)","http://example.com/$1", statusCode: StatusCodes.Status301MovedPermanently); + var builder = new WebHostBuilder() + .Configure(app => + { + app.UseRewriter(options); + }); + var server = new TestServer(builder); + + var response = await server.CreateClient().GetAsync("foo?bar=1"); + + Assert.Equal("http://example.com/foo?bar=1", response.Headers.Location.OriginalString); + } + [Theory] [InlineData(StatusCodes.Status301MovedPermanently)] [InlineData(StatusCodes.Status302Found)]