Complex input to WebApi2 function

青春壹個敷衍的年華 提交于 2019-12-12 03:19:48

问题


I've got this class in my Model:

    public class GetDocParams {
        public string LogonTicket { get; set; }
        public int CliRid { get; set; }
        public string[] ValPairs { get; set; }
        public string SortBy  { get; set; }
        public int StartRec  { get; set; }
        public int EndRec  { get; set; }
    }

This is going to be used as input to a WebApi2 function to retrieve query results from Entity Framework.

The function takes the valPairs from input and uses it to build a query that sorts by the passed pairs, i.e.

CLI_RID=111111 
DOC_NAME=Letter

would create the SQL:

WHERE CLI_RID = 111111 
 AND  DOC_NAME = 'Letter'

I'm kind of curious, how would I pass the ValPairs, using ajax and/or WebClient? GET or POST doesn't matter.


回答1:


You may have to add a new class for ValPair, like the following.

public class GetDocParams {
    public string LogonTicket { get; set; }
    public int CliRid { get; set; }
    public ValPair[] ValPairs { get; set; }
    public string SortBy  { get; set; }
    public int StartRec  { get; set; }
    public int EndRec  { get; set; }
}

public class ValPair {
    public int CLI_RID { get; set; }
    public string DOC_NAME { get; set; }
}

And you can pass values to the parameters via the following GET API call: http://www.example.com/api/docs/getDocParams?LogonTicket=111&ValPairs[0][CLI_RID]=111111&ValPairs[0][DOC_NAME]=Letter&ValPairs[1][CLI_RID]=22222&ValPairs[1][DOC_NAME]=document&....

This should work if you know the names of the keys.



来源:https://stackoverflow.com/questions/34190202/complex-input-to-webapi2-function

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