Trim leading and trailing spaces from a string using MVC Core Model Binder
In this article, I have implemented string trim model binder which would allow every post request to be processed and convert string field values to a trimmed string. Basically, it will remove leading and trailing spaces from a string.
What is Custom Model Binding?
Model binding allows controller actions to work directly with model types (passed in as method arguments), rather than HTTP requests. Mapping between incoming request data and application models is handled by model binders. Developers can extend the built-in model binding functionality by implementing custom model binders. (Reference: Microsoft documentation - Custom Model Binding in ASP.NET Core)
1- Create a Model binder
2- In MVC Core, each binder needs a provider so let’s create a Provider for the above Model Binder.
Model binder provider
3- Add MvcOptionsExtensions for Provider registration
4- Now last thing hooked this provider extension into MVC in Startup.cs
You can reach the sample code in this post from my GitHub repository.
To test sample code, run it in debug mode and access create user on below URL