-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathRequestExtensions.cs
More file actions
63 lines (55 loc) · 1.93 KB
/
Copy pathRequestExtensions.cs
File metadata and controls
63 lines (55 loc) · 1.93 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack
{
public static class RequestExtensions
{
/// <summary>
/// Duplicate Params are given a unique key by appending a #1 suffix
/// </summary>
public static Dictionary<string, string> GetRequestParams(this IRequest request)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
var map = new Dictionary<string, string>();
AddToMap(request.QueryString, map);
if ((request.Verb == HttpMethods.Post || request.Verb == HttpMethods.Put) && request.FormData != null)
{
AddToMap(request.FormData, map);
}
return map;
}
private static void AddToMap(NameValueCollection nvc, Dictionary<string, string> map)
{
for (int index = 0; index < nvc.Count; index++)
{
var name = nvc.GetKey(index);
var values = nvc.GetValues(name); // Only use string name instead of index which returns multiple values
if (name == null) //thank you .NET Framework!
{
if (values?.Length > 0)
map[values[0]] = null;
continue;
}
if (values == null || values.Length == 0)
{
map[name] = null;
}
else if (values.Length == 1)
{
map[name] = values[0];
}
else
{
for (var i = 0; i < values.Length; i++)
{
map[name + (i == 0 ? "" : "#" + i)] = values[i];
}
}
}
}
}
}