你的位置:首页 > 软件开发 > ASP.net > WebAPI 2参数绑定方法

WebAPI 2参数绑定方法

发布时间:2016-09-14 12:00:05
简单类型参数 Example 1: Sending a simple parameter in the Url [RoutePrefix("api/values")]public class ValuesController : ApiController{ ...

WebAPI 2参数绑定方法

简单类型参数

Example 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 get='_blank'>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;}

注意 [FromBody] 只能用一次,多于一次将不能正常工作

Calling the method using Urlencoded in the body:

User-Agent: FiddlerHost: localhost:49407Content-Length: 32Content-Type: application/x-www-form-urlencodedid1=1&id2=2&id3=3

WebAPI 2参数绑定方法

Calling the method using Json in the body:

User-Agent: FiddlerHost: localhost:49407Content-Length: 32Content-Type: application/json{ "Id1" : 2, "Id2": 2, "Id3": 3}
WebAPI 2参数绑定方法

 

Calling the method using in the body

This requires extra code in the Global.asax

protected void Application_Start(){  var 

 

WebAPI 2参数绑定方法

 

 

数组和列表(Array,List)

Example 6: 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<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 with 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}]

WebAPI 2参数绑定方法

Calling with :

User-Agent: FiddlerContent-Type: application/

 

WebAPI 2参数绑定方法

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


原标题:WebAPI 2参数绑定方法

关键词:web

web
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。