I am currently working on an ASP.NET Core MVC project and I am facing a problem with binding collections from IFormCollection to a JSON array.
Currently I have a web application that passes data from an IFormCollection through to other Web API microservices.
So, my form collection might look something like this pseudocode:
People[0].Name="Jon Skeet"&People[1].Name="SLaks"
When it is posted to my MVC controller as an IFormCollection, it pretty much looks the same, where the keys are "People[0].Name", "People[1].Name" and the values are "Jon Skeet" and "SLaks".
This is problematic because when I eventually serialize to JSON, instead of getting this:
{ People: [ { Name: "Jon Skeet" }, { Name: "SLaks"} ] }
I get this instead:
{ "People[0].Name": "Jon Skeet", "People[1].Name": "SLaks" }
Which does not bind to [FromBody]IEnumerable<Person> People
when I post from my MVC controller to my microservice's controller for obvious reasons.
So, my question is, how can I get my IFormCollection to serialize to JSON in such a way that indexed collections serialize properly to a JSON array? Is there some kind of custom converter or something else I need to implement in order to make it work?
Please note that my MVC controller must take an IFormCollection because it acts as a catch-all controller that routes to the proper microservice based on the Request URL.