-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathIModelBinding.cs
More file actions
104 lines (89 loc) · 2.63 KB
/
Copy pathIModelBinding.cs
File metadata and controls
104 lines (89 loc) · 2.63 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
using System;
using System.Collections.Generic;
using uhttpsharp.ModelBinders;
namespace uhttpsharp.Attributes
{
internal interface IModelBinding
{
T Get<T>(IHttpContext context, IModelBinder binder);
}
public class FromStateAttribute : Attribute, IModelBinding
{
private readonly string _propertyName;
public FromStateAttribute(string propertyName)
{
_propertyName = propertyName;
}
public T Get<T>(IHttpContext context, IModelBinder binder)
{
// Expando object
var state = (context.State as IDictionary<string,object>);
object real;
if (state != null && state.TryGetValue(_propertyName, out real) && real is T)
{
return (T)real;
}
return default(T);
}
}
public class FromBodyAttribute : PrefixAttribute
{
public FromBodyAttribute(string prefix = null) : base(prefix)
{
}
public override T Get<T>(IHttpContext context, IModelBinder binder)
{
return binder.Get<T>(context.Request.Post.Raw, Prefix);
}
}
public class FromPostAttribute : PrefixAttribute
{
public FromPostAttribute(string prefix = null)
: base(prefix)
{
}
public override T Get<T>(IHttpContext context, IModelBinder binder)
{
return binder.Get<T>(context.Request.Post.Parsed, Prefix);
}
}
public class FromQueryAttribute : PrefixAttribute
{
public FromQueryAttribute(string prefix)
: base(prefix)
{
}
public override T Get<T>(IHttpContext context, IModelBinder binder)
{
return binder.Get<T>(context.Request.QueryString, Prefix);
}
}
public class FromHeadersAttribute : PrefixAttribute
{
public FromHeadersAttribute(string prefix)
: base(prefix)
{
}
public override T Get<T>(IHttpContext context, IModelBinder binder)
{
return binder.Get<T>(context.Request.Headers, Prefix);
}
}
public abstract class PrefixAttribute : Attribute, IModelBinding
{
private readonly string _prefix;
public PrefixAttribute(string prefix)
{
_prefix = prefix;
}
public bool HasPrefix
{
get { return !string.IsNullOrEmpty(_prefix); }
}
public string Prefix
{
get { return _prefix; }
}
public abstract T Get<T>(IHttpContext context, IModelBinder binder);
}
}