forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryExtensions.cs
More file actions
47 lines (40 loc) · 1.44 KB
/
DictionaryExtensions.cs
File metadata and controls
47 lines (40 loc) · 1.44 KB
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
45
46
47
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
using System.Collections.Generic;
using System.Collections.Specialized;
namespace ServiceStack.ServiceModel
{
public static class DictionaryExtensions
{
public static Dictionary<string, string> ToDictionary(this NameValueCollection nameValues)
{
if (nameValues == null) return new Dictionary<string, string>();
var map = new Dictionary<string, string>();
foreach (var key in nameValues.AllKeys)
{
if (key == null)
{
//occurs when no value is specified, e.g. 'path/to/page?debug'
//throw new ArgumentNullException("key", "nameValues: " + nameValues);
continue;
}
var values = nameValues.GetValues(key);
if (values != null && values.Length > 0)
{
map[key] = values[0];
}
}
return map;
}
public static NameValueCollection ToNameValueCollection(this Dictionary<string, string> map)
{
if (map == null) return new NameValueCollection();
var nameValues = new NameValueCollection();
foreach (var item in map)
{
nameValues.Add(item.Key, item.Value);
}
return nameValues;
}
}
}
#endif