【问题标题】:Unsupported Media Type with Array of Int不支持的具有 Int 数组的媒体类型
【发布时间】:2020-07-20 09:39:32
【问题描述】:

我正在尝试按照此处的文档将 int 数组传递给 Net Core 3.1 API 项目的 get 方法:

https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-3.1

这表明您应该多次传递相同的参数,即 selectedCourses=1050&selectedCourses=2000

我的网址如下所示: https://localhost:5001/api/products/featured?platforms=1&platforms=2

我也试过: https://localhost:5001/api/products/featured?platforms[0]=1&platforms[1]=2

这是我的控制器方法:

        public async Task<ActionResult<List<ProductDTO>>> GetFeatured(int[] platforms)
        {
            return await _mediator.Send(new GetFeaturedProductList.Query() { Platforms = platforms });
        }

我也尝试过使用List&lt;int&gt; 而不是int[],但无济于事。

但我不断收到 415 Unsupported Media Type?

【问题讨论】:

  • 服务器 api 文档是怎么说的?每个 API 服务器都不同,您必须符合服务器要求。
  • 415 当您的服务器拒绝处理您的请求内容时返回错误。您在 Content-TypeContent-Encoding 请求标头中发送了什么?

标签: c# asp.net-core asp.net-core-webapi


【解决方案1】:

你只需要在参数前加上[FromUri],看起来像:

public async Task<ActionResult<List<ProductDTO>>> GetFeatured([FromUri] int[] platforms)
    {
        return await _mediator.Send(new GetFeaturedProductList.Query() { Platforms = platforms });
    }

网址应该是:

https://localhost:5001/api/products/featured?platforms=1&platforms=2

【讨论】:

  • 谢谢!它可以工作,但需要在 Net core 中使用 [FromQuery] 而不是 [FromURI]。谢谢!
  • @MarkTallentire 我认为您可以将 [HttpGet] 属性用于该方法
  • @DmitryKolchev 它已经有 [HttpGet("featured")] 了。我不确定为什么似乎需要 [FromQuery],因为我理解 HttpGet 也暗示了这一点。
猜你喜欢
  • 2014-05-10
  • 2019-08-02
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-25
相关资源
最近更新 更多