I'm transitioning a Web API to ASP.NET core 2.2, but having trouble model binding array data passed via query strings in GET requests. In the old web API code, jquery requests largely just worked, but now they don't. Suppose we have a simple polling API for a chat server as an example:
public class PollChat
{
public Guid ChatId { get; set; }
public int MessageId { get; set; }
}
//... in controller
[HttpGet]
[Route("api/Poll")]
public async Task<ActionResult> Poll([FromQuery] PollChat[] chats)
{
return new JsonResult(await chatServer.Poll(chats));
}
A simple jquery function to invoke this:
function poll(id, msgId) {
var data = { chats: [{ chatId: id, messageId: msgId }] };
$.get(opt.pollUrl, data, display); //'display' renders the messages
}
However, the server-side 'chats' parameter is empty. The decoded query string received server-side is:
?chats[0][chatId]=4b766187-f058-4d9e-8236-0b078657fa10&chats[0][messageId]=0
Given the model binding rules, it seems perhaps the query string should look something like:
?chats[0].chatId=4b766187-f058-4d9e-8236-0b078657fa10&chats[0].messageId=0
This works when I hard-code this request URL, but how can I get the same object serialization behaviour using jquery so I don't have to manually construct the URL? Alternately, is there a way for ASP.NET core to accept jquery's serialization format?
I've tried several revisions of the original API, and it seems like query string model binding and jquery simply do not work well together anymore, so I'm just trying to find a solution that reduces my porting effort without changing the API too drastically.