如何将数组从 Angular 6 传递到 ASP.NET Core API 以获取 GET 方法?

Sta*_*ppy 1 c# typescript asp.net-core asp.net-core-webapi angular

所以我期望的是当 Angular 6 应用程序使用 GET 方法请求时,将唯一标识符的列表或数组作为参数发送到 ASP.NET Core Web API,然后 ASP.NET Core 将带来仅与数组相关的信息返回到 angular 应用程序的唯一标识符。

我的 Web API 代码看起来像这样。

[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
}
Run Code Online (Sandbox Code Playgroud)

根据我自己的研究,我无法将唯一标识符数组添加到正文中,因为这是 GET 方法。

我必须添加 [FromQuery] 因为如果我在参数是数组或列表时不添加它会产生错误。

如果 [FromQuery] 是处理这种情况的唯一方法,那么我不知道应该如何为 Angular 编写代码。

请帮忙。

先感谢您。

Joh*_*ica 5

更改List<Guid> Guids[FromQuery]List<Guid> Guids

public ActionResult GetByGuids([FromQuery]List<Guid> Guids)
Run Code Online (Sandbox Code Playgroud)

然后,您可以将请求作为 http://myserver/controller/GetByGuids?Guids=6d627994-dce5-487e-bd2c-d48c0311a2e0&Guids=29a76d51-3c44-4fde-a0f1-b1f34567175e