为什么 MVC 查询参数用于错误的变量?

Why is MVC query parameter being used for the wrong variable?

每当我以类似于此示例的方式使用 sortfilter 作为 query 参数时,我的所有控制器端点都有问题:

[Microsoft.AspNetCore.Mvc.HttpGet, 
 Microsoft.AspNetCore.Mvc.Route("{tenantId}/archive/retrievals", Name = "getRetrievalList")]
public System.Threading.Tasks.Task<RetrievalListResult> GetRetrievalList([Microsoft.AspNetCore.Mvc.FromQuery] int? limit = null, [Microsoft.AspNetCore.Mvc.FromQuery] int? offset = null, [Microsoft.AspNetCore.Mvc.FromQuery] System.Collections.Generic.IEnumerable<string> sort = null, [Microsoft.AspNetCore.Mvc.FromQuery] System.Collections.Generic.IDictionary<string, string> filter = null)
{
    throw new NotImplementedException();
}

它与这些调用完美配合:

但是当我只使用排序时...

{{baseUrl}}/:tenantId/archive/retrievals?sort=name

...然后突然填充了 sortfilter 变量,创建了一个错误的过滤器参数。

有什么解决办法吗?

P.S。仅使用 filter=id 会导致 none 个变量被填充,这是我所期望的。

对于 字典 目标,模型绑定 查找与 parameter_name 或 property_name。如果未找到匹配项,它将查找 一种不带前缀 .

的受支持格式
  • 换句话说,“sort=name匹配支持的格式目标参数 Dictionary named filter.
  • 因此,它也会自动绑定到过滤器

您可以指定前缀来解决问题。

public IEnumerable<WeatherForecast> Get(
    [FromQuery] int? limit = null,
    [FromQuery] int? offset = null,
    [FromQuery] IEnumerable<string> sort = null,
    [FromQuery][Bind(Prefix = "filter")]IDictionary<string, string> filter = null
    )