数据输入后修剪字符串的最佳方法。我应该创建自定义模型绑定器吗?

时间:2009-11-11 22:25:40

标签: asp.net asp.net-mvc model-binders

我正在使用ASP.NET MVC,我希望所有用户输入的字符串字段在插入数据库之前进行修剪。由于我有很多数据输入表单,我正在寻找一种优雅的方法来修剪所有字符串,而不是明确地修剪每个用户提供的字符串值。我很想知道人们如何以及何时修剪字符串。

我想过可能会创建一个自定义模型绑定器并在那里修剪任何字符串值......这样,我的所有修剪逻辑都包含在一个地方。这是一个好方法吗?是否有任何代码示例可以执行此操作?

15 个答案:

答案 0 :(得分:209)

  public class TrimModelBinder : DefaultModelBinder
  {
    protected override void SetProperty(ControllerContext controllerContext, 
      ModelBindingContext bindingContext, 
      System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
    {
      if (propertyDescriptor.PropertyType == typeof(string))
      {
        var stringValue = (string)value;
        if (!string.IsNullOrWhiteSpace(stringValue))
        {
          value = stringValue.Trim();
        }
        else
        {
          value = null;
        }
      }

      base.SetProperty(controllerContext, bindingContext, 
                          propertyDescriptor, value);
    }
  }

这段代码怎么样?

ModelBinders.Binders.DefaultBinder = new TrimModelBinder();

设置global.asax Application_Start事件。

答案 1 :(得分:76)

这是@takepara相同的分辨率,但作为IModelBinder而不是DefaultModelBinder,以便在global.asax中添加modelbinder是通过

ModelBinders.Binders.Add(typeof(string),new TrimModelBinder());

班级:

public class TrimModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext,
    ModelBindingContext bindingContext)
    {
        ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (valueResult== null || valueResult.AttemptedValue==null)
           return null;
        else if (valueResult.AttemptedValue == string.Empty)
           return string.Empty;
        return valueResult.AttemptedValue.Trim();
    }
}

基于@haacked帖子: http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx

答案 2 :(得分:41)

@takepara答案的一个改进。

有些人在项目中:

public class NoTrimAttribute : Attribute { }

在TrimModelBinder类中更改

if (propertyDescriptor.PropertyType == typeof(string))

if (propertyDescriptor.PropertyType == typeof(string) && !propertyDescriptor.Attributes.Cast<object>().Any(a => a.GetType() == typeof(NoTrimAttribute)))

您可以使用[NoTrim]属性标记要从修剪中排除的属性。

答案 3 :(得分:16)

随着C#6的改进,你现在可以编写一个非常紧凑的模型绑定器来修剪所有字符串输入:

public class TrimStringModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        var attemptedValue = value?.AttemptedValue;

        return string.IsNullOrWhiteSpace(attemptedValue) ? attemptedValue : attemptedValue.Trim();
    }
}

您需要在Application_Start()文件的Global.asax.cs中的某处包含此行,以便在绑定string时使用模型绑定器:

ModelBinders.Binders.Add(typeof(string), new TrimStringModelBinder());

我发现最好使用这样的模型绑定器,而不是覆盖默认的模型绑定器,因为每当绑定string时它都会被使用,无论是直接作为方法参数还是作为方法参数模型类的属性。但是,如果您按照此处提供的其他答案覆盖默认模型绑定器,那么在模型上绑定属性时工作,当您有string动作方法的参数

编辑:评论者询问在不应验证字段时处理情况。我的原始答案被简化为只处理OP提出的问题,但对于那些感兴趣的人,您可以使用以下扩展模型绑定器来处理验证:

public class TrimStringModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var shouldPerformRequestValidation = controllerContext.Controller.ValidateRequest && bindingContext.ModelMetadata.RequestValidationEnabled;
        var unvalidatedValueProvider = bindingContext.ValueProvider as IUnvalidatedValueProvider;

        var value = unvalidatedValueProvider == null ?
          bindingContext.ValueProvider.GetValue(bindingContext.ModelName) :
          unvalidatedValueProvider.GetValue(bindingContext.ModelName, !shouldPerformRequestValidation);

        var attemptedValue = value?.AttemptedValue;

        return string.IsNullOrWhiteSpace(attemptedValue) ? attemptedValue : attemptedValue.Trim();
    }
}

答案 4 :(得分:13)

ASP.Net Core 2 中,这对我有用。我在我的控制器和JSON输入中使用[FromBody]属性。要覆盖JSON反序列化中的字符串处理,我注册了自己的JsonConverter:

services.AddMvcCore()
    .AddJsonOptions(options =>
        {
            options.SerializerSettings.Converters.Insert(0, new TrimmingStringConverter());
        })

这是转换器:

public class TrimmingStringConverter : JsonConverter
{
    public override bool CanRead => true;
    public override bool CanWrite => false;

    public override bool CanConvert(Type objectType) => objectType == typeof(string);

    public override object ReadJson(JsonReader reader, Type objectType,
        object existingValue, JsonSerializer serializer)
    {
        if (reader.Value is string value)
        {
            return value.Trim();
        }

        return reader.Value;
    }

    public override void WriteJson(JsonWriter writer, object value,
        JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

答案 5 :(得分:11)

@ takepara答案的另一个变体,但有一个不同的转折:

1)我更喜欢选择加入“StringTrim”属性机制(而不是@Anton的选择退出“NoTrim”示例)。

2)需要另外调用SetModelValue以确保正确填充ModelState并且可以正常使用默认验证/接受/拒绝模式,即应用TryUpdateModel(模型)和ModelState.Clear()以接受所有变化。

将它放在您的实体/共享库中:

/// <summary>
/// Denotes a data field that should be trimmed during binding, removing any spaces.
/// </summary>
/// <remarks>
/// <para>
/// Support for trimming is implmented in the model binder, as currently
/// Data Annotations provides no mechanism to coerce the value.
/// </para>
/// <para>
/// This attribute does not imply that empty strings should be converted to null.
/// When that is required you must additionally use the <see cref="System.ComponentModel.DataAnnotations.DisplayFormatAttribute.ConvertEmptyStringToNull"/>
/// option to control what happens to empty strings.
/// </para>
/// </remarks>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class StringTrimAttribute : Attribute
{
}

然后在你的MVC应用程序/库中:

/// <summary>
/// MVC model binder which trims string values decorated with the <see cref="StringTrimAttribute"/>.
/// </summary>
public class StringTrimModelBinder : IModelBinder
{
    /// <summary>
    /// Binds the model, applying trimming when required.
    /// </summary>
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        // Get binding value (return null when not present)
        var propertyName = bindingContext.ModelName;
        var originalValueResult = bindingContext.ValueProvider.GetValue(propertyName);
        if (originalValueResult == null)
            return null;
        var boundValue = originalValueResult.AttemptedValue;

        // Trim when required
        if (!String.IsNullOrEmpty(boundValue))
        {
            // Check for trim attribute
            if (bindingContext.ModelMetadata.ContainerType != null)
            {
                var property = bindingContext.ModelMetadata.ContainerType.GetProperties()
                    .FirstOrDefault(propertyInfo => propertyInfo.Name == bindingContext.ModelMetadata.PropertyName);
                if (property != null && property.GetCustomAttributes(true)
                    .OfType<StringTrimAttribute>().Any())
                {
                    // Trim when attribute set
                    boundValue = boundValue.Trim();
                }
            }
        }

        // Register updated "attempted" value with the model state
        bindingContext.ModelState.SetModelValue(propertyName, new ValueProviderResult(
            originalValueResult.RawValue, boundValue, originalValueResult.Culture));

        // Return bound value
        return boundValue;
    }
}

如果您没有在活页夹中设置属性值,即使您不想更改任何内容,也会完全阻止ModelState中的该属性!这是因为您注册为绑定所有字符串类型,因此它(在我的测试中)显示默认绑定器不会为您执行此操作。

答案 6 :(得分:7)

在ASP.NET Core 1.0中搜索如何执行此操作的任何人的额外信息。逻辑发生了很大的变化。

I wrote a blog post about how to do it,它更详细地解释了一些事情

所以ASP.NET Core 1.0解决方案:

模型绑定器进行实际修剪

public class TrimmingModelBinder : ComplexTypeModelBinder  
{
    public TrimmingModelBinder(IDictionary propertyBinders) : base(propertyBinders)
    {
    }

    protected override void SetProperty(ModelBindingContext bindingContext, string modelName, ModelMetadata propertyMetadata, ModelBindingResult result)
    {
        if(result.Model is string)
        {
            string resultStr = (result.Model as string).Trim();
            result = ModelBindingResult.Success(resultStr);
        }

        base.SetProperty(bindingContext, modelName, propertyMetadata, result);
    }
}

此外,您需要最新版本的Model Binder Provider,这表明此绑定器是否应该用于此模型

public class TrimmingModelBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        if (context.Metadata.IsComplexType && !context.Metadata.IsCollectionType)
        {
            var propertyBinders = new Dictionary();
            foreach (var property in context.Metadata.Properties)
            {
                propertyBinders.Add(property, context.CreateBinder(property));
            }

            return new TrimmingModelBinder(propertyBinders);
        }

        return null;
    }
}

然后它必须在Startup.cs中注册

 services.AddMvc().AddMvcOptions(options => {  
       options.ModelBinderProviders.Insert(0, new TrimmingModelBinderProvider());
 });

答案 7 :(得分:5)

在阅读上面的优秀答案和评论,并变得越来越困惑时,我突然想到,嘿,我想知道是否有jQuery解决方案。所以对于像我这样的人来说,发现ModelBinder有点令人尴尬,我提供了以下jQuery片段,在提交表单之前修剪输入字段。

    $('form').submit(function () {
        $(this).find('input:text').each(function () {
            $(this).val($.trim($(this).val()));
        })
    });

答案 8 :(得分:5)

如果是MVC Core

粘合剂:

using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using System.Threading.Tasks;
public class TrimmingModelBinder
    : IModelBinder
{
    private readonly IModelBinder FallbackBinder;

    public TrimmingModelBinder(IModelBinder fallbackBinder)
    {
        FallbackBinder = fallbackBinder ?? throw new ArgumentNullException(nameof(fallbackBinder));
    }

    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
        {
            throw new ArgumentNullException(nameof(bindingContext));
        }

        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (valueProviderResult != null &&
            valueProviderResult.FirstValue is string str &&
            !string.IsNullOrEmpty(str))
        {
            bindingContext.Result = ModelBindingResult.Success(str.Trim());
            return Task.CompletedTask;
        }

        return FallbackBinder.BindModelAsync(bindingContext);
    }
}

提供者:

using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using System;

public class TrimmingModelBinderProvider
    : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        if (!context.Metadata.IsComplexType && context.Metadata.ModelType == typeof(string))
        {
            return new TrimmingModelBinder(new SimpleTypeModelBinder(context.Metadata.ModelType));
        }

        return null;
    }
}

注册功能:

    public static void AddStringTrimmingProvider(this MvcOptions option)
    {
        var binderToFind = option.ModelBinderProviders
            .FirstOrDefault(x => x.GetType() == typeof(SimpleTypeModelBinderProvider));

        if (binderToFind == null)
        {
            return;
        }

        var index = option.ModelBinderProviders.IndexOf(binderToFind);
        option.ModelBinderProviders.Insert(index, new TrimmingModelBinderProvider());
    }

寄存器:

service.AddMvc(option => option.AddStringTrimmingProvider())

答案 9 :(得分:2)

对于 ASP.NET Core ,将ComplexTypeModelBinderProvider替换为修剪字符串的提供程序。

在您的启动代码ConfigureServices方法中,添加以下内容:

services.AddMvc()
    .AddMvcOptions(s => {
        s.ModelBinderProviders[s.ModelBinderProviders.TakeWhile(p => !(p is ComplexTypeModelBinderProvider)).Count()] = new TrimmingModelBinderProvider();
    })

像这样定义TrimmingModelBinderProvider

/// <summary>
/// Used in place of <see cref="ComplexTypeModelBinderProvider"/> to trim beginning and ending whitespace from user input.
/// </summary>
class TrimmingModelBinderProvider : IModelBinderProvider
{
    class TrimmingModelBinder : ComplexTypeModelBinder
    {
        public TrimmingModelBinder(IDictionary<ModelMetadata, IModelBinder> propertyBinders) : base(propertyBinders) { }

        protected override void SetProperty(ModelBindingContext bindingContext, string modelName, ModelMetadata propertyMetadata, ModelBindingResult result)
        {
            var value = result.Model as string;
            if (value != null)
                result = ModelBindingResult.Success(value.Trim());
            base.SetProperty(bindingContext, modelName, propertyMetadata, result);
        }
    }

    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (context.Metadata.IsComplexType && !context.Metadata.IsCollectionType) {
            var propertyBinders = new Dictionary<ModelMetadata, IModelBinder>();
            for (var i = 0; i < context.Metadata.Properties.Count; i++) {
                var property = context.Metadata.Properties[i];
                propertyBinders.Add(property, context.CreateBinder(property));
            }
            return new TrimmingModelBinder(propertyBinders);
        }
        return null;
    }
}

这个丑陋的部分是来自GetBinder的{​​{1}}逻辑的复制和粘贴,但似乎没有任何钩子让你避免这种情况。

答案 10 :(得分:1)

我不同意这个解决方案。 您应该重写GetPropertyValue,因为SetProperty的数据也可以由ModelState填充。 要从输入元素中捕获原始数据,请写下:

 public class CustomModelBinder : System.Web.Mvc.DefaultModelBinder
{
    protected override object GetPropertyValue(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, System.Web.Mvc.IModelBinder propertyBinder)
    {
        object value = base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);

        string retval = value as string;

        return string.IsNullOrWhiteSpace(retval)
                   ? value
                   : retval.Trim();
    }

}

按PropertyDescriptor PropertyType过滤,如果你真的只对字符串值感兴趣,但它并不重要,因为所有内容基本上都是一个字符串。

答案 11 :(得分:1)

晚会,但如果您要处理内置价值提供商的skipValidation要求,则以下是MVC 5.2.3所需调整的摘要。

public class TrimStringModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        // First check if request validation is required
        var shouldPerformRequestValidation = controllerContext.Controller.ValidateRequest && 
            bindingContext.ModelMetadata.RequestValidationEnabled;

        // determine if the value provider is IUnvalidatedValueProvider, if it is, pass in the 
        // flag to perform request validation (e.g. [AllowHtml] is set on the property)
        var unvalidatedProvider = bindingContext.ValueProvider as IUnvalidatedValueProvider;

        var valueProviderResult = unvalidatedProvider?.GetValue(bindingContext.ModelName, !shouldPerformRequestValidation) ??
            bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        return valueProviderResult?.AttemptedValue?.Trim();
    }
}

<强> Global.asax中

    protected void Application_Start()
    {
        ...
        ModelBinders.Binders.Add(typeof(string), new TrimStringModelBinder());
        ...
    }

答案 12 :(得分:0)

有很多帖子暗示了属性方法。这是一个已经具有trim属性的包以及许多其他包:Dado.ComponentModel.MutationsNuGet

public partial class ApplicationUser
{
    [Trim, ToLower]
    public virtual string UserName { get; set; }
}

// Then to preform mutation
var user = new ApplicationUser() {
    UserName = "   M@X_speed.01! "
}

new MutationContext<ApplicationUser>(user).Mutate();

调用Mutate()后,user.UserName将变为m@x_speed.01!

此示例将修剪空格并将字符串设置为小写。它没有引入验证,但System.ComponentModel.Annotations可以与Dado.ComponentModel.Mutations一起使用。

答案 13 :(得分:0)

我将此帖子发布到了另一个主题中。在asp.net核心2中,我朝另一个方向发展。我改用了动作过滤器。在这种情况下,开发人员可以全局设置它,也可以将其用作他/她想要应用字符串修整的动作的属性。该代码在模型绑定完成后运行,并且可以更新模型对象中的值。

这是我的代码,首先创建一个动作过滤器:

public class TrimInputStringsAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        foreach (var arg in context.ActionArguments)
        {
            if (arg.Value is string)
            {
                string val = arg.Value as string;
                if (!string.IsNullOrEmpty(val))
                {
                    context.ActionArguments[arg.Key] = val.Trim();
                }

                continue;
            }

            Type argType = arg.Value.GetType();
            if (!argType.IsClass)
            {
                continue;
            }

            TrimAllStringsInObject(arg.Value, argType);
        }
    }

    private void TrimAllStringsInObject(object arg, Type argType)
    {
        var stringProperties = argType.GetProperties()
                                      .Where(p => p.PropertyType == typeof(string));

        foreach (var stringProperty in stringProperties)
        {
            string currentValue = stringProperty.GetValue(arg, null) as string;
            if (!string.IsNullOrEmpty(currentValue))
            {
                stringProperty.SetValue(arg, currentValue.Trim(), null);
            }
        }
    }
}

要使用它,请注册为全局过滤器,或使用TrimInputStrings属性装饰您的操作。

[TrimInputStrings]
public IActionResult Register(RegisterViewModel registerModel)
{
    // Some business logic...
    return Ok();
}

答案 14 :(得分:0)

我创建了一个中间件来修剪连接字符串参数值和表单值。这已经在ASP.NET Core 3中进行了测试,并且运行良好。

public class InputCleanupMiddleware
{
    private readonly RequestDelegate _next;

    public InputCleanupMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        context.Request.Query = new QueryCollection(CleanupQueryCollection(context.Request.Query));
        if (context.Request.HasFormContentType)
            context.Request.Form = new FormCollection(CleanupQueryCollection(context.Request.Form));

        // Call the next delegate/middleware in the pipeline
        await _next(context);
    }

    private static Dictionary<string, StringValues> CleanupQueryCollection(IEnumerable<KeyValuePair<string, StringValues>> originalCollection)
    {
        var cleanCollection = new Dictionary<string, StringValues>();

        foreach (KeyValuePair<string, StringValues> originalCollectionItem in originalCollection)
        {
            string[] trimmedValues = originalCollectionItem.Value.Select(v => string.IsNullOrWhiteSpace(v) ? null : v.Trim()).ToArray();
            cleanCollection.Add(originalCollectionItem.Key, trimmedValues);
        }

        return cleanCollection;
    }
}

然后在Startup.cs的Configure()函数中注册中间件

app.UseMiddleware<InputCleanupMiddleware>();