Using ModelBinder
The binder isn't used automatically because it isn't registered in the dependency injection container and is not configured to be used within the MVC framework.
The easiest way to use this model binder is to use ModelBinderAttribute
on the argument of the action where the model should be bound:
[HttpPost] public ActionResult<object> Post( [ModelBinder(binderType: typeof(PersonsCsvBinder))] IEnumerable<Person> persons) { return new { ItemsRead = persons.Count(), Persons = persons }; }
Here, the type of our PersonsCsvBinder
is set as binderType
to that attribute.
Note
Steve Gordon wrote about a second option in his blog post, Custom ModelBinding in ASP.NET MVC Core. He uses a ModelBinderProvider
to add the ModelBinder...