0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Thank you for your help; however my question was specifically about using a GET post and passing the array of complex objects in the URL.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.