Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the REST URL syntax for passing a nested complex type?

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; }
}
like image 811
Lee Grissom Avatar asked Dec 20 '12 21:12

Lee Grissom


People also ask

How do I pass complex type in Web API?

Best way to pass multiple complex object to webapi services is by using tuple other than dynamic, json string, custom class. No need to serialize and deserialize passing object while using tuple. If you want to send more than seven complex object create internal tuple object for last tuple argument.

What is the default attribute for passing complex data type to the Web API action?

By default, Web API extracts the value of the complex type from the request body, but here, we have applied the [FromUri] attribute. So now, Web API will extract the value of the Student properties from the query string instead of the request body.

How many parameters can a get request have?

In fact, there should be no more than 5 parameters in one request, otherwise, each of them will be difficult to control from the server and browser side. If you need to transfer a large amount of information, using the POST method is recommended.


2 Answers

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

?myType.Foo.Bar=3&myType.Name=Maggie
like image 154
Maggie Ying Avatar answered Nov 15 '22 17:11

Maggie Ying


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.

like image 25
Nick Bray Avatar answered Nov 15 '22 17:11

Nick Bray