WebAPI 2 parameter binding method, webapi2 parameter binding
Simple type parametersExample 1: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"; }}
Example 2: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";}
Example 3: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";}
Example 4: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;}
Example 5:Sending an object in the Request body
[Route("example5")][HttpPost]public string GetWithBody([FromBody] ParamsObject paramsObject){ return "value:" + paramsObject.Id1;}
Note that [FromBody] can be used only once, and more than once will not work properly
Calling the method using
UrlencodedIn the body:
User-Agent: FiddlerHost: localhost:49407Content-Length: 32Content-Type: application/x-www-form-urlencodedid1=1&id2=2&id3=3
User-Agent: FiddlerHost: localhost: 49407Content-Length: 32Content-Type: application/json {"Id1": 2, "Id2": 2, "Id3": 3}Protected void Application_Start () {var xml = GlobalConfiguration. configuration. formatters. xmlFormatter; xml. useXmlSerializer = true; The client request is as follows: User-Agent: FiddlerContent-Type: application/xmlHost: localhost: 49407Content-Length: 65 <ParamsObject> <Id1> 7 </Id1> <Id2> 8 </Id2> <Id3> 9 </Id3> </ParamsObject>
// Http: /localhost: 49407/api/values/example6? ParamsObject = 2, paramsObject = 4, paramsObject = 9 [Route ("example6")] [HttpGet] public string GetListFromUri ([FromUri] List <int> paramsObject) {if (paramsObject! = Null) {return "recieved a list with length:" + paramsObject. Count;} return "nothing recieved ...";}Example 7:Sending an object list in the Body
// http://localhost:49407/api/values/example8[Route("example8")][HttpPost]public string GetListFromBody([FromBody] List<ParamsObject> paramsList){ if (paramsList != null) { return "recieved a list with length:" + paramsList.Count; } return "NOTHING RECIEVED...";}
Calling
Json:
User-Agent: FiddlerContent-Type: application/jsonHost: localhost:49407Content-Length: 91[{"Id1":3,"Id2":76,"Id3":19},{"Id1":56,"Id2":87,"Id3":94},{"Id1":976,"Id2":345,"Id3":7554}]
User-Agent: FiddlerContent-Type: application/xmlHost: localhost: 49407Content-Length: 258 <ArrayOfParamsObject> <ParamsObject> <Id1> 3 </Id1> <Id2> 76 </Id2> <Id3> 19 </Id3> </ParamsObject> <Id1> 56 </Id1> <Id2> 87 </Id2> <Id3> 94 </Id3> </ParamsObject> <Id1> 976 </Id1> <Id2> 345 </Id2> <Id3> 7554 </Id3> </ParamsObject> </ArrayOfParamsObject>
[Route ("example8")] [HttpPost] public string GetListsFromBody ([FromBody] List <ParamsObject> paramsList) {if (paramsList! = Null) {return "recieved a list with length:" + paramsList. Count;} return "nothing recieved ...";}
This is a little bit different to the previous examples. The body can only send one single object to Web API. Because of this, the lists of objects are wrapped in a list or a parent object.
POST http://localhost:49407/api/values/example8 HTTP/1.1User-Agent: FiddlerContent-Type: application/jsonHost: localhost:49407Content-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} ]]
Custom Parameters
What if the default parameter binding is not enough? Then you can use the ModelBinder class to change your parameters and create your own parameter formats. you coshould also use ActionFilters for this. when blogs exist which already explains how to use the ModelBinder class. see the links underneath.
File and binary
Files or binaries can also be sent to Web API methods. The article demonstrates how to do this.
Reference
Http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/CustomParameterBinding/
Http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
Http://www.strathweb.com/2013/04/asp-net-web-api-parameter-binding-part-1-understanding-binding-from-uri/
Http://www.roelvanlisdonk.nl /? P = 3505
Http://stackoverflow.com/questions/9981330/how-to-pass-an-array-of-integers-to-a-asp-net-web-api-rest-service
Http://stackoverflow.com/questions/14628576/passing-an-json-array-to-mvc-web-api-via-get