// 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
{
///
/// Convenience helpers to easily create specific types of parameter bindings
/// These provide a direct programmatic counterpart to the attributes.
///
public static class ParameterBindingExtensions
{
///
/// If we know statically that this binding can never succeed, then use an error binding.
/// This will prevent the action from executing.
///
/// parameter to provide binding for.
/// error message for user.
/// an error binding. Specifically, IsValid on the binding will be false.
public static HttpParameterBinding BindAsError(this HttpParameterDescriptor parameter, string message)
{
return new ErrorParameterBinding(parameter, message);
}
///
/// Bind the parameter as if it had the given attribute on the declaration.
///
/// parameter to provide binding for.
/// attribute to describe the binding.
/// a binding
public static HttpParameterBinding BindWithAttribute(this HttpParameterDescriptor parameter, ParameterBindingAttribute attribute)
{
return attribute.GetBinding(parameter);
}
///
/// 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.
///
/// parameter to provide binding for.
/// a binding
public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter)
{
return BindWithAttribute(parameter, new ModelBinderAttribute());
}
///
/// Bind the parameter using the given model binder.
///
/// parameter to provide binding for.
/// model binder to use on parameter
/// a binding
public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter, IModelBinder binder)
{
HttpConfiguration config = parameter.Configuration;
IEnumerable valueProviderFactories = new ModelBinderAttribute().GetValueProviderFactories(config);
return BindWithModelBinding(parameter, binder, valueProviderFactories);
}
///
/// Bind the parameter using default model binding but with the supplied value providers.
///
/// parameter to provide binding for.
/// value provider factories to feed to model binders
/// a binding
public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter, params ValueProviderFactory[] valueProviderFactories)
{
return BindWithModelBinding(parameter, (IEnumerable)valueProviderFactories);
}
///
/// Bind the parameter using default model binding but with the supplied value providers.
///
/// parameter to provide binding for.
/// value provider factories to feed to model binders
/// a binding
public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter, IEnumerable valueProviderFactories)
{
HttpConfiguration config = parameter.Configuration;
IModelBinder binder = new ModelBinderAttribute().GetModelBinder(config, parameter.ParameterType);
return new ModelBinderParameterBinding(parameter, binder, valueProviderFactories);
}
///
/// Bind the parameter using the supplied binder and value providers.
///
/// parameter to provide binding for.
/// model binder to use for binding.
/// value provider factories to feed to model binder.
/// a binding
public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter, IModelBinder binder, IEnumerable valueProviderFactories)
{
return new ModelBinderParameterBinding(parameter, binder, valueProviderFactories);
}
///
/// Bind the parameter from the body using the formatters from the configuration.
/// This is like having a [FromBody] attribute on the parameter
///
/// parameter to provide binding for.
/// a binding
public static HttpParameterBinding BindWithFormatter(this HttpParameterDescriptor parameter)
{
HttpConfiguration config = parameter.Configuration;
IEnumerable formatters = config.Formatters;
IBodyModelValidator validators = config.Services.GetBodyModelValidator();
return new FormatterParameterBinding(parameter, formatters, validators);
}
///
/// Bind this parameter from the body using the supplied set of formatters.
///
/// parameter to provide binding for.
/// formatters to choose from when binding the body
/// a binding
public static HttpParameterBinding BindWithFormatter(this HttpParameterDescriptor parameter, params MediaTypeFormatter[] formatters)
{
return BindWithFormatter(parameter, (IEnumerable)formatters);
}
///
/// Bind this parameter from the body using the supplied set of formatters.
///
/// parameter to provide binding for.
/// formatters to choose from when binding the body
/// a binding
public static HttpParameterBinding BindWithFormatter(this HttpParameterDescriptor parameter, IEnumerable formatters)
{
HttpConfiguration config = parameter.Configuration;
IBodyModelValidator validators = config.Services.GetBodyModelValidator();
return new FormatterParameterBinding(parameter, formatters, validators);
}
///
/// Bind this parameter from the body using the supplied set of formatters and validator.
///
/// parameter to provide binding for.
/// formatters to choose from when binding the body
/// a validator. Null to disable validation for this parameter.
/// a binding
public static HttpParameterBinding BindWithFormatter(this HttpParameterDescriptor parameter, IEnumerable formatters, IBodyModelValidator bodyModelValidator)
{
return new FormatterParameterBinding(parameter, formatters, bodyModelValidator);
}
}
}