0

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.

MB34
  • 4,210
  • 12
  • 59
  • 110
  • You really should do some sanitation on those parameters – Tamas Ionut Dec 09 '15 at 22:29
  • What do you mean by "WebClient"? – Tamas Ionut Dec 09 '15 at 22:30
  • How would you pass `ValPairs` ***where***? To another WebApi function? To a web page? From a web page? `GET` and `POST` don't do the same thing. It's not really clear what you are asking here. – Claies Dec 09 '15 at 22:43
  • Will you have any other key-value pairs other than `CLI_RID` and `DOC_NAME`? If you don't, are you able to create a new class for `ValPairs` with those two as member variables? – Jia Jian Goi Dec 10 '15 at 02:27
  • @TamasIonut, The "sanitation" is done in the WebApi2 method and if you have to ask what WebClient is, then you don't know .Net. – MB34 Dec 10 '15 at 15:02
  • @JiaJian, yes, there may be more, but I don't think we'll ever have more than 3 name/value pairss – MB34 Dec 10 '15 at 15:02
  • @Claies, "This is going to be used as input to a WebApi2". What isn't clear about that? Yes, I know that GET and POST aren't the same thing but the WebApi2 function can be written to support both. – MB34 Dec 10 '15 at 15:07

1 Answers1

0

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.

Jia Jian Goi
  • 1,415
  • 3
  • 20
  • 31
  • With some of our DOC_NAME values can be quite long, this may cause us to run into URL length limits. Using as a POST could probably do it, though. Although the ValPair class needs to be "generic" as we don't know what columns they will want to sort by. – MB34 Dec 10 '15 at 15:03
  • Hi! Wanted to know if you've manage to solve your problem? You could try having your Web API method accept a `dynamic` type parameter - you can opt not to use a model in this case. And yes, you should `POST` as a JSON array and let your Web API method deserialize it. – Jia Jian Goi Dec 24 '15 at 03:53