Files
linux-packaging-mono/external/aspnetwebstack/src/System.Web.Mvc/ByteArrayModelBinder.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

37 lines
1.3 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
namespace System.Web.Mvc
{
public class ByteArrayModelBinder : IModelBinder
{
public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException("bindingContext");
}
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
// case 1: there was no <input ... /> element containing this data
if (valueResult == null)
{
return null;
}
string value = valueResult.AttemptedValue;
// case 2: there was an <input ... /> element but it was left blank
if (String.IsNullOrEmpty(value))
{
return null;
}
// Future proofing. If the byte array is actually an instance of System.Data.Linq.Binary
// then we need to remove these quotes put in place by the ToString() method.
string realValue = value.Replace("\"", String.Empty);
return Convert.FromBase64String(realValue);
}
}
}