// Copyright (c) Microsoft Open Technologies, Inc. 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.Reflection; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.ModelBinding.Internal; using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Mvc.ModelBinding { /// /// An which binds models from the request headers when a model /// has the binding source / /// public class HeaderModelBinder : BindingSourceModelBinder { /// /// Creates a new . /// public HeaderModelBinder() : base(BindingSource.Header) { } /// protected override Task BindModelCoreAsync([NotNull] ModelBindingContext bindingContext) { var request = bindingContext.OperationBindingContext.HttpContext.Request; var modelMetadata = bindingContext.ModelMetadata; // Property name can be null if the model metadata represents a type (rather than a property or parameter). var headerName = bindingContext.BinderModelName ?? modelMetadata.PropertyName ?? bindingContext.ModelName; object model = null; if (bindingContext.ModelType == typeof(string)) { var value = request.Headers.Get(headerName); if (value != null) { model = value; } } else if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom( bindingContext.ModelType.GetTypeInfo())) { var values = request.Headers.GetCommaSeparatedValues(headerName); if (values != null) { model = ModelBindingHelper.ConvertValuesToCollectionType( bindingContext.ModelType, values); } } return Task.FromResult(new ModelBindingResult(model, bindingContext.ModelName, isModelSet: model != null)); } } }