forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpParameterDescriptorExtensions.cs
More file actions
154 lines (139 loc) · 8.03 KB
/
Copy pathHttpParameterDescriptorExtensions.cs
File metadata and controls
154 lines (139 loc) · 8.03 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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.Net.Http.Formatting;
using System.Web.Http.ModelBinding;
using System.Web.Http.Validation;
using System.Web.Http.ValueProviders;
namespace System.Web.Http.Controllers
{
/// <summary>
/// Convenience helpers to easily create specific types of parameter bindings
/// These provide a direct programmatic counterpart to the <see cref="ParameterBindingAttribute"/> attributes.
/// </summary>
public static class ParameterBindingExtensions
{
/// <summary>
/// If we know statically that this binding can never succeed, then use an error binding.
/// This will prevent the action from executing.
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <param name="message">error message for user.</param>
/// <returns>an error binding. Specifically, IsValid on the binding will be false.</returns>
public static HttpParameterBinding BindAsError(this HttpParameterDescriptor parameter, string message)
{
return new ErrorParameterBinding(parameter, message);
}
/// <summary>
/// Bind the parameter as if it had the given attribute on the declaration.
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <param name="attribute">attribute to describe the binding.</param>
/// <returns>a binding</returns>
public static HttpParameterBinding BindWithAttribute(this HttpParameterDescriptor parameter, ParameterBindingAttribute attribute)
{
return attribute.GetBinding(parameter);
}
/// <summary>
/// Bind the parameter using model binding. Get all other information from the configuration.
/// This is the same as having a plain ModelBinderAttribute on the parameter.
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <returns>a binding</returns>
public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter)
{
return BindWithAttribute(parameter, new ModelBinderAttribute());
}
/// <summary>
/// Bind the parameter using the given model binder.
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <param name="binder">model binder to use on parameter</param>
/// <returns>a binding</returns>
public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter, IModelBinder binder)
{
HttpConfiguration config = parameter.Configuration;
IEnumerable<ValueProviderFactory> valueProviderFactories = new ModelBinderAttribute().GetValueProviderFactories(config);
return BindWithModelBinding(parameter, binder, valueProviderFactories);
}
/// <summary>
/// Bind the parameter using default model binding but with the supplied value providers.
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <param name="valueProviderFactories">value provider factories to feed to model binders</param>
/// <returns>a binding</returns>
public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter, params ValueProviderFactory[] valueProviderFactories)
{
return BindWithModelBinding(parameter, (IEnumerable<ValueProviderFactory>)valueProviderFactories);
}
/// <summary>
/// Bind the parameter using default model binding but with the supplied value providers.
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <param name="valueProviderFactories">value provider factories to feed to model binders</param>
/// <returns>a binding</returns>
public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter, IEnumerable<ValueProviderFactory> valueProviderFactories)
{
HttpConfiguration config = parameter.Configuration;
IModelBinder binder = new ModelBinderAttribute().GetModelBinder(config, parameter.ParameterType);
return new ModelBinderParameterBinding(parameter, binder, valueProviderFactories);
}
/// <summary>
/// Bind the parameter using the supplied binder and value providers.
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <param name="binder">model binder to use for binding.</param>
/// <param name="valueProviderFactories">value provider factories to feed to model binder.</param>
/// <returns>a binding</returns>
public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter, IModelBinder binder, IEnumerable<ValueProviderFactory> valueProviderFactories)
{
return new ModelBinderParameterBinding(parameter, binder, valueProviderFactories);
}
/// <summary>
/// Bind the parameter from the body using the formatters from the configuration.
/// This is like having a [FromBody] attribute on the parameter
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <returns>a binding</returns>
public static HttpParameterBinding BindWithFormatter(this HttpParameterDescriptor parameter)
{
HttpConfiguration config = parameter.Configuration;
IEnumerable<MediaTypeFormatter> formatters = config.Formatters;
IBodyModelValidator validators = config.Services.GetBodyModelValidator();
return new FormatterParameterBinding(parameter, formatters, validators);
}
/// <summary>
/// Bind this parameter from the body using the supplied set of formatters.
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <param name="formatters">formatters to choose from when binding the body</param>
/// <returns>a binding</returns>
public static HttpParameterBinding BindWithFormatter(this HttpParameterDescriptor parameter, params MediaTypeFormatter[] formatters)
{
return BindWithFormatter(parameter, (IEnumerable<MediaTypeFormatter>)formatters);
}
/// <summary>
/// Bind this parameter from the body using the supplied set of formatters.
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <param name="formatters">formatters to choose from when binding the body</param>
/// <returns>a binding</returns>
public static HttpParameterBinding BindWithFormatter(this HttpParameterDescriptor parameter, IEnumerable<MediaTypeFormatter> formatters)
{
HttpConfiguration config = parameter.Configuration;
IBodyModelValidator validators = config.Services.GetBodyModelValidator();
return new FormatterParameterBinding(parameter, formatters, validators);
}
/// <summary>
/// Bind this parameter from the body using the supplied set of formatters and validator.
/// </summary>
/// <param name="parameter">parameter to provide binding for.</param>
/// <param name="formatters">formatters to choose from when binding the body</param>
/// <param name="bodyModelValidator">a validator. Null to disable validation for this parameter.</param>
/// <returns>a binding</returns>
public static HttpParameterBinding BindWithFormatter(this HttpParameterDescriptor parameter, IEnumerable<MediaTypeFormatter> formatters, IBodyModelValidator bodyModelValidator)
{
return new FormatterParameterBinding(parameter, formatters, bodyModelValidator);
}
}
}