forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelBinderParameterBinding.cs
More file actions
84 lines (70 loc) · 2.99 KB
/
Copy pathModelBinderParameterBinding.cs
File metadata and controls
84 lines (70 loc) · 2.99 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
// 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.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Metadata;
using System.Web.Http.Validation;
using System.Web.Http.ValueProviders;
using System.Web.Http.ValueProviders.Providers;
namespace System.Web.Http.ModelBinding
{
/// <summary>
/// Describes a parameter that gets bound via ModelBinding.
/// </summary>
public class ModelBinderParameterBinding : HttpParameterBinding, IValueProviderParameterBinding
{
private readonly ValueProviderFactory[] _valueProviderFactories;
private readonly IModelBinder _binder;
public ModelBinderParameterBinding(HttpParameterDescriptor descriptor,
IModelBinder modelBinder,
IEnumerable<ValueProviderFactory> valueProviderFactories)
: base(descriptor)
{
if (modelBinder == null)
{
throw Error.ArgumentNull("modelBinder");
}
if (valueProviderFactories == null)
{
throw Error.ArgumentNull("valueProviderFactories");
}
_binder = modelBinder;
_valueProviderFactories = valueProviderFactories.ToArray();
}
public IEnumerable<ValueProviderFactory> ValueProviderFactories
{
get { return _valueProviderFactories; }
}
public IModelBinder Binder
{
get { return _binder; }
}
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
{
ModelBindingContext ctx = GetModelBindingContext(metadataProvider, actionContext);
bool haveResult = _binder.BindModel(actionContext, ctx);
object model = haveResult ? ctx.Model : Descriptor.DefaultValue;
SetValue(actionContext, model);
return TaskHelpers.Completed();
}
private ModelBindingContext GetModelBindingContext(ModelMetadataProvider metadataProvider, HttpActionContext actionContext)
{
string name = Descriptor.ParameterName;
Type type = Descriptor.ParameterType;
string prefix = Descriptor.Prefix;
IValueProvider vp = CompositeValueProviderFactory.GetValueProvider(actionContext, _valueProviderFactories);
ModelBindingContext ctx = new ModelBindingContext()
{
ModelName = prefix ?? name,
FallbackToEmptyPrefix = prefix == null, // only fall back if prefix not specified
ModelMetadata = metadataProvider.GetMetadataForType(null, type),
ModelState = actionContext.ModelState,
ValueProvider = vp
};
return ctx;
}
}
}