Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 25ea93d

Browse files
author
sornaks
committed
Making QueryHelpers.AddQueryString support # in the URL.
1 parent 24f90cc commit 25ea93d

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

src/Microsoft.AspNet.WebUtilities/QueryHelpers.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,40 @@ public static class QueryHelpers
2020
/// <returns>The combined result.</returns>
2121
public static string AddQueryString([NotNull] string uri, [NotNull] string name, [NotNull] string value)
2222
{
23-
bool hasQuery = uri.IndexOf('?') != -1;
24-
return uri + (hasQuery ? "&" : "?") + UrlEncoder.Default.UrlEncode(name) + "=" + UrlEncoder.Default.UrlEncode(value);
23+
return AddQueryString(
24+
uri, new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>(name, value) });
2525
}
2626

2727
/// <summary>
2828
/// Append the given query keys and values to the uri.
2929
/// </summary>
3030
/// <param name="uri">The base uri.</param>
3131
/// <param name="queryString">A collection of name value query pairs to append.</param>
32-
/// <returns>The combine result.</returns>
32+
/// <returns>The combined result.</returns>
3333
public static string AddQueryString([NotNull] string uri, [NotNull] IDictionary<string, string> queryString)
3434
{
35+
return AddQueryString(uri, (IEnumerable<KeyValuePair<string, string>>)queryString);
36+
}
37+
38+
private static string AddQueryString(
39+
[NotNull] string uri,
40+
[NotNull] IEnumerable<KeyValuePair<string, string>> queryString)
41+
{
42+
var anchorIndex = uri.IndexOf('#');
43+
var uriToBeAppended = uri;
44+
var anchorText = "";
45+
// If there is an anchor, then the query string must be inserted before its first occurance.
46+
if (anchorIndex != -1)
47+
{
48+
anchorText = uri.Substring(anchorIndex);
49+
uriToBeAppended = uri.Substring(0, anchorIndex);
50+
}
51+
52+
var queryIndex = uriToBeAppended.IndexOf('?');
53+
var hasQuery = queryIndex != -1;
54+
3555
var sb = new StringBuilder();
36-
sb.Append(uri);
37-
bool hasQuery = uri.IndexOf('?') != -1;
56+
sb.Append(uriToBeAppended);
3857
foreach (var parameter in queryString)
3958
{
4059
sb.Append(hasQuery ? '&' : '?');
@@ -43,6 +62,8 @@ public static string AddQueryString([NotNull] string uri, [NotNull] IDictionary<
4362
sb.Append(UrlEncoder.Default.UrlEncode(parameter.Value));
4463
hasQuery = true;
4564
}
65+
66+
sb.Append(anchorText);
4667
return sb.ToString();
4768
}
4869

test/Microsoft.AspNet.WebUtilities.Tests/QueryHelpersTests.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// 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

4-
using System;
4+
using System.Collections.Generic;
55
using System.Linq;
66
using Xunit;
77

@@ -52,5 +52,63 @@ public void ParseQueryWithEmptyKeyWorks()
5252
Assert.Equal(1, collection.Count);
5353
Assert.Equal(new[] { "value1", "" }, collection[""]);
5454
}
55+
56+
[Theory]
57+
[InlineData("http://contoso.com/", "http://contoso.com/?hello=world")]
58+
[InlineData("http://contoso.com/someaction", "http://contoso.com/someaction?hello=world")]
59+
[InlineData("http://contoso.com/someaction?q=test", "http://contoso.com/someaction?q=test&hello=world")]
60+
[InlineData(
61+
"http://contoso.com/someaction?q=test#anchor",
62+
"http://contoso.com/someaction?q=test&hello=world#anchor")]
63+
[InlineData("http://contoso.com/someaction#anchor", "http://contoso.com/someaction?hello=world#anchor")]
64+
[InlineData("http://contoso.com/#anchor", "http://contoso.com/?hello=world#anchor")]
65+
[InlineData(
66+
"http://contoso.com/someaction?q=test#anchor?value",
67+
"http://contoso.com/someaction?q=test&hello=world#anchor?value")]
68+
[InlineData(
69+
"http://contoso.com/someaction#anchor?stuff",
70+
"http://contoso.com/someaction?hello=world#anchor?stuff")]
71+
[InlineData(
72+
"http://contoso.com/someaction?name?something",
73+
"http://contoso.com/someaction?name?something&hello=world")]
74+
[InlineData(
75+
"http://contoso.com/someaction#name#something",
76+
"http://contoso.com/someaction?hello=world#name#something")]
77+
public void AddQueryStringWithKeyAndValue(string uri, string expectedUri)
78+
{
79+
var result = QueryHelpers.AddQueryString(uri, "hello", "world");
80+
Assert.Equal(expectedUri, result);
81+
}
82+
83+
[Theory]
84+
[InlineData("http://contoso.com/", "http://contoso.com/?hello=world&some=text")]
85+
[InlineData("http://contoso.com/someaction", "http://contoso.com/someaction?hello=world&some=text")]
86+
[InlineData("http://contoso.com/someaction?q=1", "http://contoso.com/someaction?q=1&hello=world&some=text")]
87+
[InlineData("http://contoso.com/some#action", "http://contoso.com/some?hello=world&some=text#action")]
88+
[InlineData("http://contoso.com/some?q=1#action", "http://contoso.com/some?q=1&hello=world&some=text#action")]
89+
[InlineData("http://contoso.com/#action", "http://contoso.com/?hello=world&some=text#action")]
90+
[InlineData(
91+
"http://contoso.com/someaction?q=test#anchor?value",
92+
"http://contoso.com/someaction?q=test&hello=world&some=text#anchor?value")]
93+
[InlineData(
94+
"http://contoso.com/someaction#anchor?stuff",
95+
"http://contoso.com/someaction?hello=world&some=text#anchor?stuff")]
96+
[InlineData(
97+
"http://contoso.com/someaction?name?something",
98+
"http://contoso.com/someaction?name?something&hello=world&some=text")]
99+
[InlineData(
100+
"http://contoso.com/someaction#name#something",
101+
"http://contoso.com/someaction?hello=world&some=text#name#something")]
102+
public void AddQueryStringWithDictionary(string uri, string expectedUri)
103+
{
104+
var queryStrings = new Dictionary<string, string>()
105+
{
106+
{ "hello", "world" },
107+
{ "some", "text" }
108+
};
109+
110+
var result = QueryHelpers.AddQueryString(uri, queryStrings);
111+
Assert.Equal(expectedUri, result);
112+
}
55113
}
56114
}

0 commit comments

Comments
 (0)