0

Assume that I have Web API method:

    [HttpGet]
    public string SomeAction([FromUri] ObjectA a, [FromUri] ObjectB b)
    {
        return Ok("test");
    }

How to call it using HttpClient? What is the best way to prepare appropriate query string for objects A and B?

UPDATE

ObjectA and ObjectB are complex objects... e.g.

public class ObjectA 
{
    public string Prop1 { get; set; }
    public int Prop2 { get; set; }
    public double Prop3 { get; set; }
}

I can prepare query string for every specific case, but I'm interested to get any universal method that would allow to work with any objects...

For now I see only one possible solution - using reflection go through the list of properties and build query string. I assume that there should be some already implemented mechanism... am I wrong?

Mr. Pumpkin
  • 6,212
  • 6
  • 44
  • 60
  • 1
    [Link](http://stackoverflow.com/questions/17096201/build-query-string-for-system-net-httpclient-get) This might be helpful – MustangManiac May 30 '14 at 19:55

1 Answers1

0

If you're including complex objects on the query string, I will assume they are being encoded in some manner, from what you're trying to do, it would have to be a default encoding or you won't be able to put them on the query string.

Normal routing would be:

/{controller}/{action}/{a}/{b}

or

/{controller}/{action}?a={content}&b={content}

Dependent upon how you have your routes configured and your preference on using query string parameters as opposed to "friendly" urls.

Alternately, the MVC framework will attempt to populate your object from values present in any available data location (i.e. query string, cookies, et. al.) where variable names = property names. In this case, you could use something like this:

/SomeController/SomeAction?objectaProp1=abc&objectaProp2=def...

Hope this helps.

SoftSan
  • 2,482
  • 3
  • 23
  • 54
Lemiarty
  • 124
  • 10