// 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.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Metadata;
using System.Web.Http.Properties;
using System.Web.Http.Validation;
namespace System.Web.Http.ModelBinding
{
///
/// Parameter binding that will read from the body and invoke the formatters.
///
public class FormatterParameterBinding : HttpParameterBinding
{
// Magic key to pass cancellation token through the request property bag to maintain backward compat.
private const string CancellationTokenKey = "MS_FormatterParameterBinding_CancellationToken";
private IEnumerable _formatters;
private string _errorMessage;
public FormatterParameterBinding(HttpParameterDescriptor descriptor, IEnumerable formatters, IBodyModelValidator bodyModelValidator)
: base(descriptor)
{
if (descriptor.IsOptional)
{
_errorMessage = Error.Format(SRResources.OptionalBodyParameterNotSupported, descriptor.Prefix ?? descriptor.ParameterName, GetType().Name);
}
Formatters = formatters;
BodyModelValidator = bodyModelValidator;
}
public override bool WillReadBody
{
get { return true; }
}
public override string ErrorMessage
{
get
{
return _errorMessage;
}
}
public IEnumerable Formatters
{
get { return _formatters; }
set
{
if (value == null)
{
throw Error.ArgumentNull("formatters");
}
_formatters = value;
}
}
public IBodyModelValidator BodyModelValidator
{
get;
set;
}
public virtual Task