Web API Model binding - Complex Parameter

228 Views Asked by At

I have a string received in query string

f[0][p]=zap&f[0][v][] = 110&f[0][v][]=500&f[1][p] = prih&f[1][v][] = 10000000&f[1][v][] = 30000000000

I try to catch it with string[] but it is always null. How to represent this parameter to Web API so it can serialize it?

I have a workaround with reading it for Uri as a string and then parse it but it is not good for unit testing and would like to update it.

Is it even possible with structure like this?

1

There are 1 best solutions below

0
On

Ok, I solved this, maybe it can help someone.

I needed a class to represent the received object :

 public class Filter
 {
    private string p { get; set; }
    private string[] v { get; set; }
    private string cp { get; set; }
    private string[] cv { get; set; }
 }

And in method definition I needed:

public SubjectStats BusinessSubjects(string country, string start, string end, [FromUri] Filter[] f)

The key was to define object as Filter[] in method signature.