diff --git a/Refit.Tests/RequestBuilder.cs b/Refit.Tests/RequestBuilder.cs index 074853af0..656833acf 100644 --- a/Refit.Tests/RequestBuilder.cs +++ b/Refit.Tests/RequestBuilder.cs @@ -2531,7 +2531,7 @@ public void ParametersShouldBePutAsExplicitQueryString() var uri = new Uri(new Uri("http://api"), output.RequestUri); - Assert.Equal("/query?q2=value2&q1=value1", uri.PathAndQuery); + Assert.Equal("/query?q1=value1&q2=value2", uri.PathAndQuery); } [Fact] diff --git a/Refit/RequestBuilderImplementation.cs b/Refit/RequestBuilderImplementation.cs index 0d5dece62..cc4d88aef 100644 --- a/Refit/RequestBuilderImplementation.cs +++ b/Refit/RequestBuilderImplementation.cs @@ -980,17 +980,7 @@ param as IDictionary // UriBuilder business so that we preserve any hardcoded query // parameters as well as add the parameterized ones. var uri = new UriBuilder(new Uri(new Uri("http://api"), urlTarget)); - var query = HttpUtility.ParseQueryString(uri.Query ?? ""); - foreach (var key in query.AllKeys) - { - if (!string.IsNullOrWhiteSpace(key)) - { - queryParamsToAdd.Insert( - 0, - new KeyValuePair(key, query[key]) - ); - } - } + ParseExistingQueryString(uri, queryParamsToAdd); if (queryParamsToAdd.Count != 0) { @@ -1100,6 +1090,25 @@ var value in ParseEnumerableQueryParameterValue( } } + static void ParseExistingQueryString(UriBuilder uri, List> queryParamsToAdd) + { + if (string.IsNullOrEmpty(uri.Query)) + return; + + var query = HttpUtility.ParseQueryString(uri.Query); + var index = 0; + foreach (var key in query.AllKeys) + { + if (!string.IsNullOrWhiteSpace(key)) + { + queryParamsToAdd.Insert( + index++, + new KeyValuePair(key, query[key]) + ); + } + } + } + static string CreateQueryString(List> queryParamsToAdd) { // Suppress warning as ValueStringBuilder.ToString calls Dispose()