// 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; using System.Threading.Tasks; using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Mvc.ModelBinding { /// /// ModelBinder to bind Byte Arrays. /// public class ByteArrayModelBinder : IModelBinder { /// public async Task BindModelAsync([NotNull] ModelBindingContext bindingContext) { if (bindingContext.ModelType != typeof(byte[])) { return null; } var valueProviderResult = await bindingContext.ValueProvider.GetValueAsync(bindingContext.ModelName); // case 1: there was no element containing this data if (valueProviderResult == null) { return null; } var value = valueProviderResult.AttemptedValue; // case 2: there was an element but it was left blank if (string.IsNullOrEmpty(value)) { return null; } try { var model = Convert.FromBase64String(value); return new ModelBindingResult(model, bindingContext.ModelName, isModelSet: true); } catch (Exception ex) { bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, ex); } // Matched the type (byte[]) only this binder supports. // Always tell the model binding system to skip other model binders i.e. return non-null. return new ModelBindingResult(model: null, key: bindingContext.ModelName, isModelSet: false); } } }