This repository was archived by the owner on Dec 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathSeparatedQueryStringValueProvider.cs
More file actions
49 lines (41 loc) · 1.56 KB
/
SeparatedQueryStringValueProvider.cs
File metadata and controls
49 lines (41 loc) · 1.56 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
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
namespace WebAPI
{
public class SeparatedQueryStringValueProvider : QueryStringValueProvider
{
private readonly string _key;
private readonly string _separator;
private readonly IQueryCollection _values;
public SeparatedQueryStringValueProvider(IQueryCollection values, string separator) : this(null, values, separator)
{
}
public SeparatedQueryStringValueProvider(string key, IQueryCollection values, string separator) : base(BindingSource.Query, values, CultureInfo.InvariantCulture)
{
_key = key;
_values = values;
_separator = separator;
}
public override ValueProviderResult GetValue(string key)
{
var result = base.GetValue(key);
if (_key != null && _key != key)
{
return result;
}
if (result != ValueProviderResult.None && result.Values.Any(x => x.IndexOf(_separator, StringComparison.OrdinalIgnoreCase) > 0))
{
var splitValues = new StringValues(result.Values
.SelectMany(x => x.Split(new[] { _separator }, StringSplitOptions.None)).ToArray());
return new ValueProviderResult(splitValues, result.Culture);
}
return result;
}
}
}