-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathRequestBindingExtensions.cs
More file actions
151 lines (143 loc) · 6.45 KB
/
Copy pathRequestBindingExtensions.cs
File metadata and controls
151 lines (143 loc) · 6.45 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using Microsoft.AspNetCore.Routing;
namespace Microsoft.AspNetCore.Http
{
/// <summary>
/// Various extension methods for parsing request data from routes, query strings and and the form
/// </summary>
public static class RequestBindingExtensions
{
/// <summary>
/// Attempts to represent the specified route parameter as the specified type.
/// </summary>
/// <typeparam name="TValue">The target type to convert to.</typeparam>
/// <param name="routeValues">The route values</param>
/// <param name="name">The route parameter name</param>
/// <param name="value">The result of the conversion was successful.</param>
/// <returns>true if the parse operation was successful; otherwise, false.</returns>
public static bool TryGet<TValue>(this RouteValueDictionary routeValues, string name, out TValue value)
{
return TryGet(routeValues, (d, k) => (string)d[k], name, out value);
}
/// <summary>
/// Attempts to represent the specified route parameter as the specified type.
/// </summary>
/// <typeparam name="TValue">The target type to convert to.</typeparam>
/// <param name="routeValues">The route values</param>
/// <param name="name">The route parameter name</param>
/// <returns>A tuple of the converted value and a bool that determined is the operation was successful.</returns>
public static (TValue Value, bool Ok) Get<TValue>(this RouteValueDictionary routeValues, string name)
{
var ok = routeValues.TryGet(name, out TValue value);
return (value, ok);
}
/// <summary>
/// Attempts to represent the specified query string parameter as the specified type.
/// </summary>
/// <typeparam name="TValue">The target type to convert to.</typeparam>
/// <param name="query">The query string values</param>
/// <param name="name">The query string parameter name</param>
/// <param name="value">The result of the conversion was successful.</param>
/// <returns>true if the parse operation was successful; otherwise, false.</returns>
public static bool TryGet<TValue>(this IQueryCollection query, string name, out TValue value)
{
return TryGet(query, (d, k) => d[k], name, out value);
}
/// <summary>
/// Attempts to represent the specified query string parameter as the specified type.
/// </summary>
/// <typeparam name="TValue">The target type to convert to.</typeparam>
/// <param name="query">The query string values</param>
/// <param name="name">The query string parameter name</param>
/// <returns>A tuple of the converted value and a bool that determined is the operation was successful.</returns>
public static (TValue Value, bool Ok) Get<TValue>(this IQueryCollection query, string name)
{
var ok = query.TryGet(name, out TValue value);
return (value, ok);
}
/// <summary>
/// Attempts to represent the specified form parameter as the specified type.
/// </summary>
/// <typeparam name="TValue">The target type to convert to.</typeparam>
/// <param name="form">The form values</param>
/// <param name="name">The form parameter name</param>
/// <param name="value">The result of the conversion was successful.</param>
/// <returns>true if the parse operation was successful; otherwise, false.</returns>
public static bool TryGet<TValue>(this IFormCollection form, string name, out TValue value)
{
return TryGet(form, (d, k) => d[k], name, out value);
}
/// <summary>
/// Attempts to represent the specified form parameter as the specified type.
/// </summary>
/// <typeparam name="TValue">The target type to convert to.</typeparam>
/// <param name="form">The form values</param>
/// <param name="name">The form parameter name</param>
/// <returns>A tuple of the converted value and a bool that determined is the operation was successful.</returns>
public static (TValue Value, bool Ok) TryGet<TValue>(this IFormCollection form, string name)
{
var ok = form.TryGet(name, out TValue value);
return (value, ok);
}
private static bool TryGet<TValue, TState>(TState lookup, Func<TState, string, string> get, string name, out TValue value)
{
if (typeof(TValue) == typeof(int))
{
if (int.TryParse(get(lookup, name), out var intVal))
{
value = (TValue)(object)intVal;
return true;
}
}
else if (typeof(TValue) == typeof(string))
{
value = (TValue)(object)get(lookup, name);
return value != null;
}
else if (typeof(TValue) == typeof(Guid))
{
if (Guid.TryParse(get(lookup, name), out var guidVal))
{
value = (TValue)(object)guidVal;
return true;
}
}
// TODO: DateTime (which format), TimeSpan
// Less common
else if (typeof(TValue) == typeof(decimal))
{
if (decimal.TryParse(get(lookup, name), out var decimalVal))
{
value = (TValue)(object)decimalVal;
return true;
}
}
else if (typeof(TValue) == typeof(long))
{
if (long.TryParse(get(lookup, name), out var longVal))
{
value = (TValue)(object)longVal;
return true;
}
}
else if (typeof(TValue) == typeof(short))
{
if (short.TryParse(get(lookup, name), out var shortVal))
{
value = (TValue)(object)shortVal;
return true;
}
}
else if (typeof(TValue) == typeof(byte))
{
if (byte.TryParse(get(lookup, name), out var byteVal))
{
value = (TValue)(object)byteVal;
return true;
}
}
value = default;
return false;
}
}
}