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

时间:2017-08-02 03:58:43

标签: c# asp.net-core 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属性并且没有工作。 网址查询如下所示:

  ?

/ API /用户参数[0] = .PropertyName姓&安培;参数[0]。价值= GTitzy&安培;参数[0] .FilterCondition = 0

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

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

1 个答案:

答案 0 :(得分:2)

假设

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

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

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

您还应该考虑阅读Model Binding: Customize model binding behavior with attributes

相关问题