forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeModelBinder.cs
More file actions
88 lines (75 loc) · 3.6 KB
/
Copy pathCompositeModelBinder.cs
File metadata and controls
88 lines (75 loc) · 3.6 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
// 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.Web.Http.Controllers;
namespace System.Web.Http.ModelBinding.Binders
{
/// <summary>
/// This class is an <see cref="IModelBinder"/> that delegates to one of a collection of
/// <see cref="ModelBinderProvider"/> instances.
/// </summary>
/// <remarks>
/// If no binder is available and the <see cref="ModelBindingContext"/> allows it,
/// this class tries to find a binder using an empty prefix.
/// </remarks>
public class CompositeModelBinder : IModelBinder
{
public CompositeModelBinder(IEnumerable<IModelBinder> binders)
: this(binders.ToArray())
{
}
public CompositeModelBinder(params IModelBinder[] binders)
{
Binders = binders;
}
private IModelBinder[] Binders { get; set; }
public virtual bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
ModelBindingContext newBindingContext = CreateNewBindingContext(bindingContext, bindingContext.ModelName);
bool boundSuccessfully = TryBind(actionContext, newBindingContext);
if (!boundSuccessfully && !String.IsNullOrEmpty(bindingContext.ModelName)
&& bindingContext.FallbackToEmptyPrefix)
{
// fallback to empty prefix?
newBindingContext = CreateNewBindingContext(bindingContext, modelName: String.Empty);
boundSuccessfully = TryBind(actionContext, newBindingContext);
}
if (!boundSuccessfully)
{
return false; // something went wrong
}
// run validation and return the model
// If we fell back to an empty prefix above and are dealing with simple types,
// propagate the non-blank model name through for user clarity in validation errors.
// Complex types will reveal their individual properties as model names and do not require this.
if (!newBindingContext.ModelMetadata.IsComplexType && String.IsNullOrEmpty(newBindingContext.ModelName))
{
newBindingContext.ValidationNode = new Validation.ModelValidationNode(newBindingContext.ModelMetadata, bindingContext.ModelName);
}
newBindingContext.ValidationNode.Validate(actionContext, null /* parentNode */);
bindingContext.Model = newBindingContext.Model;
return true;
}
private bool TryBind(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
return actionContext.Bind(bindingContext, Binders);
}
private static ModelBindingContext CreateNewBindingContext(ModelBindingContext oldBindingContext, string modelName)
{
ModelBindingContext newBindingContext = new ModelBindingContext
{
ModelMetadata = oldBindingContext.ModelMetadata,
ModelName = modelName,
ModelState = oldBindingContext.ModelState,
ValueProvider = oldBindingContext.ValueProvider
};
// validation is expensive to create, so copy it over if we can
if (Object.ReferenceEquals(modelName, oldBindingContext.ModelName))
{
newBindingContext.ValidationNode = oldBindingContext.ValidationNode;
}
return newBindingContext;
}
}
}