kreta/Kreta.Core/ModelBinder/StringTrimModelBinder.cs
2024-03-13 00:33:46 +01:00

18 lines
578 B
C#

using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
namespace Kreta.Core.ModelBinder
{
public class StringTrimModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var attemptedValue = value?.AttemptedValue;
bindingContext.Model = string.IsNullOrWhiteSpace(attemptedValue) ? null : attemptedValue.Trim();
return true;
}
}
}