通过查询字符串将复杂对象传递给 .netcore1.1 Webapi

Passing Complex Object to .netcore1.1 Webapi though Query string

我试图通过查询字符串传递一个复杂的对象,但由于某种原因它不起作用。我有一个看起来像这样的复杂对象:

public class QueryOptions
{
    public QueryParameter[] Parameters = new QueryParameter[0];
}

我尝试了几种发送方式,但没有任何效果:

我的 webapi 方法如下所示:

[HttpGet]
[AllowAnonymous]
public async Task<TDTO[]> GetList([FromQuery] QueryOptions queryOptions)
{
    return await this._service.GetList(queryOptions);
}

我试过使用和不使用 FromQuery 属性都不起作用。 url 查询如下所示:

/api/users?Parameters[0].PropertyName=FirstName&Parameters[0].Value=GTitzy&Parameters[0].FilterCondition=0

我也试过将对象的名称附加到开头。请求已发送,但 queryOptions 始终没有参数。

如何通过查询字符串传递这个复杂的对象?

假设

public class QueryParameter {
    public string PropertyName { get; set; }
    public string Value { get; set; }
    public string FilterCondition { get; set; }
}

您需要更新模型以公开 [FromQuery] 的 public 属性以了解要绑定到的内容。

public class QueryOptions {
    public QueryParameter[] Parameters { get; set; }
}

你也应该考虑阅读Model Binding: Customize model binding behavior with attributes