This is pretty much a reblog from http://damienbod.com/2014/08/22/web-api-2-exploring-parameter-binding/ so the full credit goes there too.
Posted here as i find it useful and it's way easier for me to rememeber one url ;)
anyway, a couple of nice examples of param binding in web api 2
- Sending a simple parameter in the Url
[RoutePrefix("api/values")]
public class ValuesController : ApiController
{
// http://localhost:49407/api/values/example1?id=2
[Route("example1")]
[HttpGet]
public string Get(int id)
{
return "value";
}
}
- Sending simple parameters in the Url
// http://localhost:49407/api/values/example2?id1=1&id2=2&id3=3
[Route("example2")]
[HttpGet]
public string GetWith3Parameters(int id1, long id2, double id3)
{
return "value";
}
- Sending simple parameters using attribute routing
// http://localhost:49407/api/values/example3/2/3/4
[Route("example3/{id1}/{id2}/{id3}")]
[HttpGet]
public string GetWith3ParametersAttributeRouting(int id1, long id2, double id3)
{
return "value";
}
- Sending an object in the Url
// http://localhost:49407/api/values/example4?id1=1&id2=2&id3=3
[Route("example4")]
[HttpGet]
public string GetWithUri([FromUri] ParamsObject paramsObject)
{
return "value:" + paramsObject.Id1;
}
- Sending an object in the Request body
[Route("example5")]
[HttpPost]
public string GetWithBody([FromBody] ParamsObject paramsObject)
{
return "value:" + paramsObject.Id1;
}
- Calling the method using Urlencoded in the body
User-Agent: Fiddler
Host: localhost:49407
Content-Length: 32
Content-Type: application/x-www-form-urlencoded
id1=1&id2=2&id3=3
Calling the method using Json in the body
User-Agent: Fiddler
Host: localhost:49407
Content-Length: 32
Content-Type: application/json
{ "Id1" : 2, "Id2": 2, "Id3": 3}
Calling the method using XML in the body (requires some extra code in Global.asax
protected void Application_Start()
{
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;
User-Agent: Fiddler
Content-Type: application/xml
Host: localhost:49407
Content-Length: 65
<ParamsObject><Id1>7</Id1><Id2>8</Id2><Id3>9</Id3></ParamsObject>
- Sending a simple list in the Url
// http://localhost:49407/api/values/example6?paramsObject=2,paramsObject=4,paramsObject=9
[Route("example6")]
[HttpGet]
public string GetListFromUri([FromUri] List paramsObject)
{
if (paramsObject != null)
{
return "recieved a list with length:" + paramsObject.Count;
}
return "NOTHING RECIEVED...";
}
- Sending an object list in the Body
// http://localhost:49407/api/values/example8
[Route("example8")]
[HttpPost]
public string GetListFromBody([FromBody] List paramsList)
{
if (paramsList != null)
{
return "recieved a list with length:" + paramsList.Count;
}
return "NOTHING RECIEVED...";
}
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:49407
Content-Length: 91
[{"Id1":3,"Id2":76,"Id3":19},{"Id1":56,"Id2":87,"Id3":94},{"Id1":976,"Id2":345,"Id3":7554}]
Calling with XML
User-Agent: Fiddler
Content-Type: application/xml
Host: localhost:49407
Content-Length: 258
<ArrayOfParamsObject>
<ParamsObject><Id1>3</Id1><Id2>76</Id2><Id3>19</Id3></ParamsObject>
<ParamsObject><Id1>56</Id1><Id2>87</Id2><Id3>94</Id3></ParamsObject>
<ParamsObject><Id1>976</Id1><Id2>345</Id2><Id3>7554</Id3></ParamsObject>
</ArrayOfParamsObject>
- Sending object lists in the Body
[Route("example8")]
[HttpPost]
public string GetListsFromBody([FromBody] List> paramsList)
{
if (paramsList != null)
{
return "recieved a list with length:" + paramsList.Count;
}
return "NOTHING RECIEVED...";
}
POST http://localhost:49407/api/values/example8 HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:49407
Content-Length: 185
[
[
{"Id1":3,"Id2":76,"Id3":19},
{"Id1":56,"Id2":87,"Id3":94},
{"Id1":976,"Id2":345,"Id3":7554}
],
[
{"Id1":3,"Id2":76,"Id3":19},
{"Id1":56,"Id2":87,"Id3":94},
{"Id1":976,"Id2":345,"Id3":7554}
]
]