12

What's the URL syntax for passing an object with a nested object to my ASP.NET Web API GET method? Is this possible? http://mydomain/mycontroller?...

Mycontroller GET method:

 public void Get([FromUri]MyType myType) { ... }

C# types:

public class MyType
{
  public string Name { get; set; }
  public NestedType Foo { get; set; }
}

public class NestedType
{
  public int Bar { get; set; }
}
Lee Grissom
  • 9,705
  • 6
  • 37
  • 47
  • This looks like an interesting related discussion: http://stackoverflow.com/questions/7104578/rest-complex-composite-nested-resources – JayC Dec 20 '12 at 22:03

2 Answers2

15

It is possible -- try passing the URL in this format:

?myType.Foo.Bar=3&myType.Name=Maggie
Maggie Ying
  • 10,095
  • 2
  • 33
  • 36
  • 1
    Thanks Maggie, your answer is perfect. – Lee Grissom Dec 20 '12 at 22:23
  • What happens when you try to do something similar in the case where `MyType` has the following property: `public IEnumerable Foos { get; set; }` ?? :O I just tried it and can't figure out what to put in the querystring :( I've done this before for complex types that had collections of simple types (`?MyType.MyString=firstElement&MyType.MyString=secondElement`), but never complex types that had collections of other complex types! :( – mo. Oct 09 '13 at 16:39
  • Woah. I just found your answer here: http://stackoverflow.com/a/13963155/326110 You rock! – mo. Oct 09 '13 at 17:02
1

If you're trying to implement a get that performs the following: 1) get by name 2) get by Foo.Bar

Then you could use querystring parameters. REST pass multiple inputs to GET method

If you are not really trying to do a GET and instead you are trying to POST data to the server, then you should use a POST.

Community
  • 1
  • 1
Nick Bray
  • 1,953
  • 12
  • 18
  • While Lee specifically mentions GET in his question, the spirit of the question technically applies regardless of the HTTP verb. If we were discussing his controller's Post() method, and he wanted (or needed) to accept parameters passed in the URI, the situation would be identical. Saying "just use POST" doesn't answer the question so much as skirt around it. – mo. Oct 09 '13 at 16:12