简体   繁体   中英

How do I pass array from Angular 6 to ASP.NET Core API for GET method?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM