你的位置:首页 > ASP.net教程

[ASP.net教程]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 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

webapiparams_01

Calling the method using Json in the body:

User-Agent: FiddlerHost: localhost:49407Content-Length: 32Content-Type: application/json{ "Id1" : 2, "Id2": 2, "Id3": 3}
webapiparams_02

 

Calling the method using in the body

This requires extra code in the Global.asax

protected void Application_Start(){  var 

 

webapiparams_03

 

 

数组和列表(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}]

webapiparams_05_json

Calling with :

User-Agent: FiddlerContent-Type: application/

 

毛里求斯旅游费用多少毛里求斯旅游哪里好玩毛里求斯旅游行程深圳到毛里求斯旅游深圳去毛里求斯旅游报价十大过早 感受武汉美食的独特魅力 连山金子山在哪里坐车?金子山有直达车吗? 从化石门森林公园梅花是真的吗?石门森林公园梅花什么时候最好看? 来武汉不可不吃的十大早点小吃 白沙龙母庙放生祈福活动时间?肇庆白沙龙母庙放生祈福活动什么时候? 端芬岭南小九寨好玩吗?台山端芬岭南小九寨简介? 端芬岭南小九寨旅游攻略?台山端芬岭南小九寨门票多少钱? 岭南小九寨电话?台山岭南小九寨门票网上可以预订吗? 茶马古道上的“枢纽”—四方街 首尔江南区购物血拼时尚之旅 纳西族的神山—玉龙雪山 金沙遗址博物馆景区免费直通车 横店影视城图片 西樵山在哪里_西樵山在什么地方 天津美食推荐_天津小吃介绍_天津美食大全_天津有哪些小吃 黑龙江漠河风景图片_北极光图片_冬天图片 HL-10103QGC Datasheet HL-10103QGC Datasheet HL-10103QYC Datasheet HL-10103QYC Datasheet HL-10103SRDT Datasheet HL-10103SRDT Datasheet 乐途旅游网 乐途旅游网 乐途旅游网 暑假旅游推荐 暑假旅游推荐 暑假旅游推荐 旅游多少钱 旅游多少钱 旅游多少钱