Making QueryHelpers.AddQueryString support # in the URL.
This commit is contained in:
parent
24f90cc914
commit
25ea93de9e
|
|
@ -20,8 +20,8 @@ namespace Microsoft.AspNet.WebUtilities
|
||||||
/// <returns>The combined result.</returns>
|
/// <returns>The combined result.</returns>
|
||||||
public static string AddQueryString([NotNull] string uri, [NotNull] string name, [NotNull] string value)
|
public static string AddQueryString([NotNull] string uri, [NotNull] string name, [NotNull] string value)
|
||||||
{
|
{
|
||||||
bool hasQuery = uri.IndexOf('?') != -1;
|
return AddQueryString(
|
||||||
return uri + (hasQuery ? "&" : "?") + UrlEncoder.Default.UrlEncode(name) + "=" + UrlEncoder.Default.UrlEncode(value);
|
uri, new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>(name, value) });
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -29,12 +29,31 @@ namespace Microsoft.AspNet.WebUtilities
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="uri">The base uri.</param>
|
/// <param name="uri">The base uri.</param>
|
||||||
/// <param name="queryString">A collection of name value query pairs to append.</param>
|
/// <param name="queryString">A collection of name value query pairs to append.</param>
|
||||||
/// <returns>The combine result.</returns>
|
/// <returns>The combined result.</returns>
|
||||||
public static string AddQueryString([NotNull] string uri, [NotNull] IDictionary<string, string> queryString)
|
public static string AddQueryString([NotNull] string uri, [NotNull] IDictionary<string, string> queryString)
|
||||||
{
|
{
|
||||||
|
return AddQueryString(uri, (IEnumerable<KeyValuePair<string, string>>)queryString);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string AddQueryString(
|
||||||
|
[NotNull] string uri,
|
||||||
|
[NotNull] IEnumerable<KeyValuePair<string, string>> queryString)
|
||||||
|
{
|
||||||
|
var anchorIndex = uri.IndexOf('#');
|
||||||
|
var uriToBeAppended = uri;
|
||||||
|
var anchorText = "";
|
||||||
|
// If there is an anchor, then the query string must be inserted before its first occurance.
|
||||||
|
if (anchorIndex != -1)
|
||||||
|
{
|
||||||
|
anchorText = uri.Substring(anchorIndex);
|
||||||
|
uriToBeAppended = uri.Substring(0, anchorIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
var queryIndex = uriToBeAppended.IndexOf('?');
|
||||||
|
var hasQuery = queryIndex != -1;
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append(uri);
|
sb.Append(uriToBeAppended);
|
||||||
bool hasQuery = uri.IndexOf('?') != -1;
|
|
||||||
foreach (var parameter in queryString)
|
foreach (var parameter in queryString)
|
||||||
{
|
{
|
||||||
sb.Append(hasQuery ? '&' : '?');
|
sb.Append(hasQuery ? '&' : '?');
|
||||||
|
|
@ -43,6 +62,8 @@ namespace Microsoft.AspNet.WebUtilities
|
||||||
sb.Append(UrlEncoder.Default.UrlEncode(parameter.Value));
|
sb.Append(UrlEncoder.Default.UrlEncode(parameter.Value));
|
||||||
hasQuery = true;
|
hasQuery = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sb.Append(anchorText);
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
// 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.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -52,5 +52,63 @@ namespace Microsoft.AspNet.WebUtilities
|
||||||
Assert.Equal(1, collection.Count);
|
Assert.Equal(1, collection.Count);
|
||||||
Assert.Equal(new[] { "value1", "" }, collection[""]);
|
Assert.Equal(new[] { "value1", "" }, collection[""]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("http://contoso.com/", "http://contoso.com/?hello=world")]
|
||||||
|
[InlineData("http://contoso.com/someaction", "http://contoso.com/someaction?hello=world")]
|
||||||
|
[InlineData("http://contoso.com/someaction?q=test", "http://contoso.com/someaction?q=test&hello=world")]
|
||||||
|
[InlineData(
|
||||||
|
"http://contoso.com/someaction?q=test#anchor",
|
||||||
|
"http://contoso.com/someaction?q=test&hello=world#anchor")]
|
||||||
|
[InlineData("http://contoso.com/someaction#anchor", "http://contoso.com/someaction?hello=world#anchor")]
|
||||||
|
[InlineData("http://contoso.com/#anchor", "http://contoso.com/?hello=world#anchor")]
|
||||||
|
[InlineData(
|
||||||
|
"http://contoso.com/someaction?q=test#anchor?value",
|
||||||
|
"http://contoso.com/someaction?q=test&hello=world#anchor?value")]
|
||||||
|
[InlineData(
|
||||||
|
"http://contoso.com/someaction#anchor?stuff",
|
||||||
|
"http://contoso.com/someaction?hello=world#anchor?stuff")]
|
||||||
|
[InlineData(
|
||||||
|
"http://contoso.com/someaction?name?something",
|
||||||
|
"http://contoso.com/someaction?name?something&hello=world")]
|
||||||
|
[InlineData(
|
||||||
|
"http://contoso.com/someaction#name#something",
|
||||||
|
"http://contoso.com/someaction?hello=world#name#something")]
|
||||||
|
public void AddQueryStringWithKeyAndValue(string uri, string expectedUri)
|
||||||
|
{
|
||||||
|
var result = QueryHelpers.AddQueryString(uri, "hello", "world");
|
||||||
|
Assert.Equal(expectedUri, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("http://contoso.com/", "http://contoso.com/?hello=world&some=text")]
|
||||||
|
[InlineData("http://contoso.com/someaction", "http://contoso.com/someaction?hello=world&some=text")]
|
||||||
|
[InlineData("http://contoso.com/someaction?q=1", "http://contoso.com/someaction?q=1&hello=world&some=text")]
|
||||||
|
[InlineData("http://contoso.com/some#action", "http://contoso.com/some?hello=world&some=text#action")]
|
||||||
|
[InlineData("http://contoso.com/some?q=1#action", "http://contoso.com/some?q=1&hello=world&some=text#action")]
|
||||||
|
[InlineData("http://contoso.com/#action", "http://contoso.com/?hello=world&some=text#action")]
|
||||||
|
[InlineData(
|
||||||
|
"http://contoso.com/someaction?q=test#anchor?value",
|
||||||
|
"http://contoso.com/someaction?q=test&hello=world&some=text#anchor?value")]
|
||||||
|
[InlineData(
|
||||||
|
"http://contoso.com/someaction#anchor?stuff",
|
||||||
|
"http://contoso.com/someaction?hello=world&some=text#anchor?stuff")]
|
||||||
|
[InlineData(
|
||||||
|
"http://contoso.com/someaction?name?something",
|
||||||
|
"http://contoso.com/someaction?name?something&hello=world&some=text")]
|
||||||
|
[InlineData(
|
||||||
|
"http://contoso.com/someaction#name#something",
|
||||||
|
"http://contoso.com/someaction?hello=world&some=text#name#something")]
|
||||||
|
public void AddQueryStringWithDictionary(string uri, string expectedUri)
|
||||||
|
{
|
||||||
|
var queryStrings = new Dictionary<string, string>()
|
||||||
|
{
|
||||||
|
{ "hello", "world" },
|
||||||
|
{ "some", "text" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = QueryHelpers.AddQueryString(uri, queryStrings);
|
||||||
|
Assert.Equal(expectedUri, result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue