1

So what I'm expecting is when Angular 6 app request with GET Method, sending a list or array of unique identifier as a parameter to the ASP.NET Core web API, then ASP.NET Core will bring information only relevant to the array of unique identifier back to the angular app.

My web API code looks something like this.

[HttpGet]
public ActionResult GetByGuids(List<Guid> Guids)
{
  // some code here...

  return Ok(_someService.someAction(Guids)); 
  //Guids will be passed as a parameter to the function of service layer
}

According to my own research, I cannot add the array of unique identifier to the body because this is GET method.

I have to add [FromQuery] since it produces an error if I don't add it when a parameter is array or list.

If [FromQuery] is the only way to deal with this situation, then I have no idea how I should write the code for Angular.

Please help.

Thank you in advance.

Staytrippy
  • 41
  • 2
  • 7
  • Change to `IEnumerable Guids` and pass as `?Guids=6d627994-dce5-487e-bd2c-d48c0311a2e0&Guids=29a76d51-3c44-4fde-a0f1-b1f34567175e`, etc? – ProgrammingLlama Dec 14 '18 at 00:51
  • _"since it produces an error if I don't add it"_ - please make sure you include your error messages when you get errors. – ProgrammingLlama Dec 14 '18 at 00:54
  • Does this answer your question? [Pass an array of integers to ASP.NET Web API?](https://stackoverflow.com/questions/9981330/pass-an-array-of-integers-to-asp-net-web-api) – Michael Freidgeim Oct 25 '20 at 20:19

1 Answers1

6

Change List<Guid> Guids to [FromQuery]List<Guid> Guids:

public ActionResult GetByGuids([FromQuery]List<Guid> Guids)

You can then make the request as http://myserver/controller/GetByGuids?Guids=6d627994-dce5-487e-bd2c-d48c0311a2e0&Guids=29a76d51-3c44-4fde-a0f1-b1f34567175e

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • I ran into a problem with passing "array" like query string params to a ASP.NET Core 2.1 Api where the array would always come empty. See here for an answer that fixes the issue: https://stackoverflow.com/questions/43397851/pass-array-into-asp-net-core-route-query-string – jpgrassi Dec 14 '18 at 12:57
  • Thanks for the answer. But you should add name on FromQuery in order to work in ASP.NET Core 2.1 – Staytrippy Dec 17 '18 at 02:05
  • I just upgraded my own project to 2.1, but still don't need to add name on `FromQuery` unless the name passed differs. – ProgrammingLlama Dec 17 '18 at 02:14