forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJQueryMVCFormUrlEncodedFormatter.cs
More file actions
76 lines (64 loc) · 2.38 KB
/
JQueryMVCFormUrlEncodedFormatter.cs
File metadata and controls
76 lines (64 loc) · 2.38 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading.Tasks;
namespace System.Web.Http.ModelBinding
{
// Supports JQuery schema on FormURL.
public class JQueryMvcFormUrlEncodedFormatter : FormUrlEncodedMediaTypeFormatter
{
private readonly HttpConfiguration _configuration = null;
public JQueryMvcFormUrlEncodedFormatter()
{
}
public JQueryMvcFormUrlEncodedFormatter(HttpConfiguration config)
{
_configuration = config;
}
public override bool CanReadType(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
return true;
}
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (readStream == null)
{
throw new ArgumentNullException("readStream");
}
// For simple types, defer to base class
if (base.CanReadType(type))
{
return base.ReadFromStreamAsync(type, readStream, content, formatterLogger);
}
return ReadFromStreamAsyncCore(type, readStream, content, formatterLogger);
}
private async Task<object> ReadFromStreamAsyncCore(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
object obj = await base.ReadFromStreamAsync(typeof(FormDataCollection), readStream, content, formatterLogger);
FormDataCollection fd = (FormDataCollection)obj;
try
{
return fd.ReadAs(type, String.Empty, RequiredMemberSelector, formatterLogger, _configuration);
}
catch (Exception e)
{
if (formatterLogger == null)
{
throw;
}
formatterLogger.LogError(String.Empty, e);
return GetDefaultValueForType(type);
}
}
}
}