-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathQueryStringHelper.cs
44 lines (41 loc) · 1.49 KB
/
QueryStringHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
namespace HybridWebView
{
internal static class QueryStringHelper
{
public static string RemovePossibleQueryString(string? url)
{
if (string.IsNullOrEmpty(url))
{
return string.Empty;
}
var indexOfQueryString = url.IndexOf('?', StringComparison.Ordinal);
return (indexOfQueryString == -1)
? url
: url.Substring(0, indexOfQueryString);
}
// TODO: Replace this
/// <summary>
/// A simple utility that takes a URL, extracts the query string and returns a dictionary of key-value pairs.
/// Note that values are unescaped. Manually created URLs in JavaScript should use encodeURIComponent to escape values.
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static Dictionary<string, string> GetKeyValuePairs(string? url)
{
var result = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(url))
{
var query = new Uri(url).Query;
if (query != null && query.Length > 1)
{
result = query
.Substring(1)
.Split('&')
.Select(p => p.Split('='))
.ToDictionary(p => p[0], p => Uri.UnescapeDataString(p[1]));
}
}
return result;
}
}
}