Files
Umbraco-CMS/src/Umbraco.Web/WebApi/TrimModelBinder.cs
2018-11-22 14:05:51 +00:00

24 lines
757 B
C#

using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
namespace Umbraco.Web.WebApi
{
/// <summary>
/// A model binder to trim the string
/// </summary>
internal class TrimModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (valueResult?.AttemptedValue == null)
{
return false;
}
bindingContext.Model = (string.IsNullOrWhiteSpace(valueResult.AttemptedValue) ? valueResult.AttemptedValue : valueResult.AttemptedValue.Trim());
return true;
}
}
}