Pass a list of complex object in query string to WEB API

隐身守侯 提交于 2019-12-12 10:10:05

问题


I have an WEB API method that uses [FromUri] to bind complex type object to my view model, and in this view model, I have a list of complex object inside it. How do I populate this list when I do a GET request?

This is my method from WEB API:

[HttpGet]
public HttpResponseMessage ListPaged([FromUri]PaginationReParams parameters)
{
   // DO SOMETHING HERE...
}

The PaginationReqParams view model

public class PaginationReqParams
    {
        public PaginationReqParams()
        {
            this.Filters = new List<FiltersReq>();
        }

        public List<FiltersReq> Filters { get; set; }
        public Int32 Page { get; set; }
        public Int32 PageSize { get; set; }
    }

The FiltersReq class

public class FiltersReq 
    {
        public String Field { get; set; }
        public String Value { get; set; }
        public String ComparisonOperator { get; set; }
    }

When I pass parameters to my query string Like "page" it binds normally, but how do I do to bind the "Filters" parameter?


回答1:


Pass the parameters like this:

?page=1&pagesize=10&filters[0].Field=name&filters[0].Value=aladdin&filters[0].ComparisonOperator=eq&filters[1].Field=age&filters[1].Value=18&filters[1].ComparisonOperator=eq


来源:https://stackoverflow.com/questions/47931625/pass-a-list-of-complex-object-in-query-string-to-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!