David Walker

Trimming all strings submitted to ASP.NET

by David Walker

"Usernames cannot end with a space." You had the time to code that error message, but not the time to call trim()?
    @shanselman

 In my C#/ASP.NET applications, I add the following TrimModelBinder. This trims every single string that is sent to the server before my controllers even get them. Before we implemented it, we did a small case study and found no instances of it being necessary to submit leading or trailing spaces. Since we have had it implemented, we have not once needed to work around it. Your mileage may vary.

To use, register it in application_start in global.asax or application initialization code that is called from there.

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

and here is the model binder code. It implements IModelBinder and trims submitted strings, if they are found.

public class TrimModelBinder : IModelBinder
{
    /// <summary>
    /// Binds the model to a value by using the specified controller context and binding context.
    /// </summary>
    /// <param name="controllerContext" />The controller context.
    /// <param name="bindingContext" />The binding context.
    /// <returns>The bound value.</returns>
    public object BindModel(ControllerContext controllerContext,    ModelBindingContext bindingContext)
    {
        var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (valueResult == null || valueResult.AttemptedValue == null)
        {
            return null;
        }

        return (String.IsNullOrWhiteSpace(valueResult.AttemptedValue) ? 
            valueResult.AttemptedValue : 
            valueResult.AttemptedValue.Trim());
    }
}

David Walker

David Walker is a Software Consultant, Photographer, and Digital Artist based out of Orlando, Florida, USA.

He believes in secure reliable software and productive happy teams.

More ...