forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleModelBinderProvider.cs
More file actions
97 lines (84 loc) · 3.16 KB
/
Copy pathSimpleModelBinderProvider.cs
File metadata and controls
97 lines (84 loc) · 3.16 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
// 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.Diagnostics.Contracts;
using System.Web.Http.Controllers;
namespace System.Web.Http.ModelBinding.Binders
{
// Returns a user-specified binder for a given type.
public sealed class SimpleModelBinderProvider : ModelBinderProvider
{
private readonly Func<IModelBinder> _modelBinderFactory;
private readonly Type _modelType;
public SimpleModelBinderProvider(Type modelType, IModelBinder modelBinder)
{
if (modelType == null)
{
throw Error.ArgumentNull("modelType");
}
if (modelBinder == null)
{
throw Error.ArgumentNull("modelBinder");
}
_modelType = modelType;
_modelBinderFactory = () => modelBinder;
}
public SimpleModelBinderProvider(Type modelType, Func<IModelBinder> modelBinderFactory)
{
if (modelType == null)
{
throw Error.ArgumentNull("modelType");
}
if (modelBinderFactory == null)
{
throw Error.ArgumentNull("modelBinderFactory");
}
_modelType = modelType;
_modelBinderFactory = modelBinderFactory;
}
public Type ModelType
{
get { return _modelType; }
}
public bool SuppressPrefixCheck { get; set; }
public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType)
{
if (modelType == null)
{
throw Error.ArgumentNull("modelType");
}
if (modelType == ModelType)
{
if (SuppressPrefixCheck)
{
// If we're suppressing a prefix check, then we don't need any further info from the ActionContext
// to know that we're using this binder.
return _modelBinderFactory();
}
else
{
return new SimpleModelBinder(this);
}
}
return null;
}
// Helper binder to do the prefix check before invoking into the user's binder.
private class SimpleModelBinder : IModelBinder
{
private readonly SimpleModelBinderProvider _parent;
public SimpleModelBinder(SimpleModelBinderProvider parent)
{
_parent = parent;
}
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
Contract.Assert(!_parent.SuppressPrefixCheck); // wouldn't have even created this binder
if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
{
IModelBinder binder = _parent._modelBinderFactory();
return binder.BindModel(actionContext, bindingContext);
}
return false;
}
}
}
}