-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQueryStringHelper.cs
463 lines (405 loc) · 15.3 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web;
namespace Diplo.Helpers
{
/// <summary>
/// Used to parse and manipulate an HTTP querystring [GET request]
/// </summary>
public class QueryStringHelper
{
/// <summary>
/// Gets the current query string collection
/// </summary>
public NameValueCollection QueryStringCollection { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="QueryStringHelper"/> class using the passed querystring
/// </summary>
/// <param name="qs">The querystring to use</param>
public QueryStringHelper(string qs)
{
this.QueryStringCollection = HttpUtility.ParseQueryString(qs);
}
/// <summary>
/// Initializes a new instance of the <see cref="QueryStringHelper"/> class using the querystring in the current HttpContext.Request
/// </summary>
public QueryStringHelper()
{
if (HttpContext.Current == null || HttpContext.Current.Request == null)
throw new NullReferenceException("There is no HttpContext or the Request is null");
this.QueryStringCollection = new NameValueCollection(HttpContext.Current.Request.QueryString);
}
/// <summary>
/// Initializes a new instance of the <see cref="QueryStringHelper"/> class from an HttpRequest
/// </summary>
/// <param name="request">The HTTP request to use</param>
public QueryStringHelper(HttpRequest request)
{
this.QueryStringCollection = new NameValueCollection(request.QueryString);
}
/// <summary>
/// Initializes a new instance of the <see cref="QueryStringHelper"/> class from a NameValueCollection
/// </summary>
/// <param name="nvCollection">The name value collection</param>
public QueryStringHelper(NameValueCollection nvCollection)
{
this.QueryStringCollection = new NameValueCollection(nvCollection);
}
/// <summary>
/// Checks whether a key with the name exists
/// </summary>
/// <param name="name">The name to check</param>
/// <returns>True if it exists or false if it doesn't</returns>
public bool NameExists(string name)
{
return !String.IsNullOrEmpty(GetValueByName(name));
}
/// <summary>
/// Gets a querystring value by name
/// </summary>
/// <param name="name">The name of the key to get</param>
/// <returns>The string value associated with the key</returns>
public string GetValueByName(string name)
{
return QueryStringCollection.Get(name);
}
/// <summary>
/// Gets a string value by name and if it is null or empty returns a default value instead
/// </summary>
/// <param name="name">The value</param>
/// <param name="defaultValue">The default value to return when null or empty</param>
/// <returns>A value</returns>
public string GetValueByName(string name, string defaultValue)
{
string value = QueryStringCollection.Get(name);
if (!String.IsNullOrEmpty(value))
return value;
else
return defaultValue;
}
/// <summary>
/// Gets a querystring value and converts it to the destination type
/// </summary>
/// <typeparam name="T">The type to convert it to</typeparam>
/// <param name="name">The name of the key to get from the query string</param>
/// <returns>The value of the key converted to a type of T</returns>
/// <exception cref="System.FormatException">Throws a System.FormatException if the type cannot be converted</exception>
public T GetValueByName<T>(string name) where T : struct
{
string value = GetValueByName(name);
if (String.IsNullOrEmpty(value))
{
return default(T);
}
return (T)Convert.ChangeType(value, typeof(T));
}
/// <summary>
/// Gets a querystring value and converts it to the destination type using the converter
/// </summary>
/// <typeparam name="T">The type to convert the value to</typeparam>
/// <param name="name">The name of the key to get from the query string</param>
/// <param name="converter">The delegate to perform the conversion</param>
/// <param name="throwOnError">Whether to throw an exception when there is an error converting (default = false)</param>
/// <returns>The value of the key converted to a type of T</returns>
/// <example>var cats = qs.GetMultipleValuesByName<int>("cat", x => Convert.ToInt32(x))</example>
public T GetValueByName<T>(string name, Func<string, T> converter, bool throwOnError = false)
{
if (converter == null)
throw new ArgumentNullException("converter", "The delegate converter cannot be null");
string value = GetValueByName(name);
if (String.IsNullOrEmpty(value) && throwOnError == false)
{
return default(T);
}
try
{
return converter(value);
}
catch (Exception)
{
if (throwOnError)
throw;
else
return default(T);
}
}
/// <summary>
/// Gets a querystring value and converts it to the destination type. If it cannot be converted then the defaultValue is returned instead.
/// </summary>
/// <typeparam name="T">The type to convert it to</typeparam>
/// <param name="name">The name of the key to get</param>
/// <param name="defaultValue">The default value to return if it cannot be converted</param>
/// <returns>The value of the key converted to a type of T or the default value</returns>
public T GetValueByName<T>(string name, T defaultValue) where T : struct
{
if (String.IsNullOrEmpty(GetValueByName(name)))
{
return defaultValue;
}
try
{
return GetValueByName<T>(name);
}
catch (FormatException)
{
return defaultValue;
}
}
/// <summary>
/// Gets the named querystring value multiple times as a string collection
/// </summary>
/// <param name="name">The name</param>
/// <returns>An enumerable (which can be empty if there are no matches)</returns>
public IEnumerable<string> GetMultipleValuesByName(string name)
{
var vals = QueryStringCollection.GetValues(name);
if (vals == null)
{
return Enumerable.Empty<string>();
}
else
{
return vals;
}
}
/// <summary>
/// Gets multiple query string values for the given name (eg. ?cat=123&cat=434&cat=454)
/// </summary>
/// <typeparam name="T">The Type you want to convert the value to</typeparam>
/// <param name="name">The name of the query string</param>
/// <returns>An enumerable of type T. If a value cannot be converted it is not returned.</returns>
public IEnumerable<T> GetMultipleValuesByName<T>(string name) where T : struct
{
var vals = QueryStringCollection.GetValues(name);
if (vals == null)
{
return Enumerable.Empty<T>();
}
else
{
var list = new List<T>();
foreach (var value in vals)
{
if (!String.IsNullOrEmpty(value))
{
T converted;
try
{
converted = (T)Convert.ChangeType(value, typeof(T));
list.Add(converted);
}
catch (FormatException)
{
// do nothing
}
}
}
return list;
}
}
/// <summary>
/// Gets multiple query string values for the given name (eg. ?cat=123&cat=434&cat=454)
/// </summary>
/// <typeparam name="T">The Type you want to convert the value to</typeparam>
/// <param name="name">The name of the query string</param>
/// <param name="defaultValue">A default value that is returned if it cannot be converted</param>
/// <returns>An enumerable of type T. If a value cannot be converted then default value is returned instead.</returns>
public IEnumerable<T> GetMultipleValuesByName<T>(string name, T defaultValue) where T : struct
{
var vals = QueryStringCollection.GetValues(name);
if (vals == null)
{
return Enumerable.Empty<T>();
}
else
{
var list = new List<T>();
foreach (var value in vals)
{
if (!String.IsNullOrEmpty(value))
{
T converted;
try
{
converted = (T)Convert.ChangeType(value, typeof(T));
}
catch (FormatException)
{
converted = defaultValue;
}
list.Add(converted);
}
}
return list;
}
}
/// <summary>
/// Gets multiple query string values for the given name (eg. ?cat=123&cat=434&cat=454)
/// </summary>
/// <typeparam name="T">The Type you want to convert the value to</typeparam>
/// <param name="name">The name of the query string</param>
/// <param name="converter">A delegate to perform the conversion</param>
/// <param name="throwOnError">Whether to throw an exception when an error occurs during conversion</param>
/// <returns>An enumerable of type T</returns>
public IEnumerable<T> GetMultipleValuesByName<T>(string name, Func<string, T> converter, bool throwOnError = false)
{
if (converter == null)
throw new ArgumentNullException("converter", "The delegate converter cannot be null");
var vals = QueryStringCollection.GetValues(name);
if (vals == null)
{
return Enumerable.Empty<T>();
}
else
{
var list = new List<T>();
foreach (var value in vals)
{
try
{
list.Add(converter(value));
}
catch (Exception)
{
if (throwOnError)
throw;
}
}
return list;
}
}
/// <summary>
/// Adds the specified name and value to the querystring. If the value already exists it is added with a comma.
/// </summary>
/// <param name="name">The name to add</param>
/// <param name="value">The value to add</param>
public void Add(string name, string value)
{
QueryStringCollection.Add(name, value);
}
/// <summary>
/// Adds the specified name and value (converted to a string) to the querystring. If the value already exists it is added with a comma.
/// </summary>
/// <param name="name">The name to add</param>
/// <param name="value">The value to add</param>
public void Add(string name, object value)
{
this.Add(name, value.ToString());
}
/// <summary>
/// Adds a new value or replaces it if a key with that value already exists
/// </summary>
/// <param name="name">The name to add</param>
/// <param name="value">The value to add</param>
public void AddOrReplace(string name, string value)
{
QueryStringCollection.Set(name, value);
}
/// <summary>
/// Adds a new value or replaces it if a key with that value already exists. The value is converted to a string.
/// </summary>
/// <param name="name">The name to add</param>
/// <param name="value">The value to add</param>
public void AddOrReplace(string name, object value)
{
AddOrReplace(name, value.ToString());
}
/// <summary>
/// Removes the key with the name
/// </summary>
/// <param name="name">The name of the key to remove</param>
public void RemoveByName(string name)
{
QueryStringCollection.Remove(name);
}
/// <summary>
/// Removes all the keys that have a specific value
/// </summary>
/// <param name="value">The value to check for</param>
/// <returns>A count of the keys removed</returns>
public int RemoveByValue(string value)
{
int removed = 0;
for (int i = QueryStringCollection.Keys.Count - 1; i > 0; i--)
{
if (QueryStringCollection.Get(i) == value)
{
QueryStringCollection.Remove(QueryStringCollection.Keys[i]);
removed++;
}
}
return removed;
}
/// <summary>
/// Clears this querystring
/// </summary>
public void Clear()
{
QueryStringCollection.Clear();
}
/// <summary>
/// Returns a count of the keys in the querystring
/// </summary>
/// <returns>A positive integer or 0 if no values</returns>
public int Count()
{
return QueryStringCollection.Count;
}
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current querystring
/// </summary>
/// <returns>
/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </returns>
public override string ToString()
{
return GetQueryString();
}
/// <summary>
/// Gets the querystring as a string of name and value pairs delimited with an ampersand
/// </summary>
/// <returns>The querystring as a string</returns>
public string GetQueryString()
{
return GetQueryString(false);
}
/// <summary>
/// Gets the querystring as a string of name and value pairs delimited with an ampersand
/// </summary>
/// <param name="removeEmpty">If set to <c>true</c> keys with no value are removed</param>
/// <returns>The querystring as a string with empty keys removed</returns>
public string GetQueryString(bool removeEmpty)
{
StringBuilder qs = new StringBuilder();
int len = QueryStringCollection.Keys.Count;
for (int i = 0; i < len; i++)
{
string val = QueryStringCollection.Get(i);
if (String.IsNullOrEmpty(val) && removeEmpty)
continue;
string key = HttpUtility.UrlEncode(QueryStringCollection.GetKey(i));
var items = QueryStringCollection.Get(i).Split(','); // required as Get returns CSV for values with same key
foreach (var value in items)
{
qs.AppendFormat("{0}={1}&", key, HttpUtility.UrlEncode(value));
}
}
if (len > 0)
{
qs.Length -= 1;
}
return qs.ToString();
}
/// <summary>
/// Converts the querystring NameValueCollection to a generic Dictionary that can be easily interated over
/// </summary>
/// <returns>A Dictionary of string name and value pairs</returns>
public Dictionary<string, string> ToDictionary()
{
return this.QueryStringCollection.Cast<string>().Select(s => new { Key = s, Value = this.QueryStringCollection[s] }).ToDictionary(p => p.Key, p => p.Value);
}
}
}