猿问

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

所以我期待的是,当 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

}

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


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


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


请帮忙。


守候你守候我
浏览 162回答 1
1回答

Smart猫小萌

更改List<Guid> Guids为[FromQuery]List<Guid> Guids:public&nbsp;ActionResult&nbsp;GetByGuids([FromQuery]List<Guid>&nbsp;Guids)然后,您可以将请求作为http://myserver/controller/GetByGuids?Guids=6d627994-dce5-487e-bd2c-d48c0311a2e0&Guids=29a76d51-3c44-4fde-a0f1-b1f34567175e
随时随地看视频慕课网APP
我要回答