【问题标题】:IEnumerable as URI-parameter to NET Core APIIEnumerable 作为 NET Core API 的 URI 参数
【发布时间】:2020-12-01 02:14:11
【问题描述】:

在 .NET Core 3.x 中接收 IEnumerable 作为 GET 端点的 URI 参数的最佳方式是什么

在更深层次的背景下:我想做的是根据 GUID 的数量过滤掉数据。

目前为止

    [HttpGet]
    public ActionResult Get(int allowedMissing, IEnumerable<Guid> ids)
    .
    .
    .

我使用 Postman 调用端点,如下所示: /endpoint?allowedMissing=2&amp;ids=cab0cfcb-8eb2-4313-8064-e7b0841d4f8a&amp;ids=b2b944c2-6b4a-4c32-9a2f-472b891e4843

这会导致415 Unsupported Media Type,而不会在控制器内遇到任何断点。

【问题讨论】:

    标签: api asp.net-core .net-core get postman


    【解决方案1】:

    好的,所以在 Action 中接收 IEnumerable 的默认值似乎在 body 中,如果作为查询发送,则需要使用 FromQuery 装饰参数。

    改成这个解决了我的问题:

    [HttpGet]
    public ActionResult Get(int allowedMissing, [FromQuery] IEnumerable<Guid> ids)
    .
    .
    .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2020-02-08
      • 2021-02-07
      • 2020-10-29
      相关资源
      最近更新 更多