Reputation: 3289
I am able to pass arrays of simple types in the URI (i.e. GET requests). I am also able to pass complex objects in the URI.
However, I have not been able to pass an array of complex objects.
If I have a class, say,
class Person {
public FirstName { get; set;}
public LastName { get; set; }
}
And my Web API action method's parameter is an IEnumerable<Person>
, how should the URI look like? If it helps, I am using JavaScript to make the GET request.
Upvotes: 0
Views: 271
Reputation: 8336
You might want to include your controller method. But from what you are describing a little bit of code for a model binder would make it fairly easy for you to pass a list of people in any format you want in either the querystring or as part of the posted body content.
Here's a link to asp.net core model binding that is current at the time of this writing. https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1 It'll probably stop working someday but you can always use your favorite search engine to look for model binder
and get current info.
Upvotes: 1
Reputation: 1084
You can submit your collection of objects using JSON.
var lstPersons = [
{ FirstName: "John", LastName: "Doe" },
{ FirstName: "Bob", LastName: "Sams" },
{ FirstName: "Jane", LastName: "Doe" }
];
$.ajax({
url: "/api/person",
data: JSON.stringify({ pos: lstPersons }) ,
contentType: 'application/json, charset=utf-8'
type: 'POST',
});
To deserialize your JSON object in your web api you can use the documentation below for guidance.
Deserialize with CustomCreationConverter
Upvotes: 0