-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiParametersJsonValueProvider.cs
48 lines (46 loc) · 1.56 KB
/
MultiParametersJsonValueProvider.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.IO;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Newtonsoft.Json.Linq;
namespace MultiParametersWebApi
{
public class MultiParametersJsonValueProvider : IValueProvider
{
private JContainer _container;
public MultiParametersJsonValueProvider(ValueProviderFactoryContext context)
{
using (StreamReader reader = new StreamReader(context.ActionContext.HttpContext.Request.Body))
{
string debug = reader.ReadToEnd().Trim();
_container = debug.StartsWith("[", StringComparison.OrdinalIgnoreCase) ?
(JContainer)JArray.Parse(debug) :
(JContainer)JObject.Parse(debug);
}
}
public bool ContainsPrefix(string prefix)
{
JToken token = _container.SelectToken(prefix);
return token != null;
}
public ValueProviderResult GetValue(string key)
{
JToken token = _container.SelectToken(key);
if (token == null)
{
ValueProviderResult s = new ValueProviderResult(new StringValues(string.Empty));
return s;
}
else
{
ValueProviderResult s = new ValueProviderResult(new StringValues(token.Value<string>()));
return s;
}
}
}
}