WebApi 之请求参数

在学习webApi请求过程中,发现参数的传递是一个比较重要的过程,初学习时,感觉自己弄懂了,然后测试好像ok。但换了一个环境后,又发现自己蒙了。故而想到整理下这其中的请求情况。现在列出一些表格如下:

[HttpGet]

Type

 ajax request Data

request contentType 

api parameter 

remarks 

 get   primary type-->multiply parameter:data:{id:1,name:'zhaosw'}-->Json object  none  get(int id,string name) the samplest request for ajax
 get object type-->one parameter:data:{id:2,name:'zhaosw',age:'25'}-->Json object  none  get([fromUri] User user)  request for object
 get  object type-->one parameter:data:{strQuery:JSON.stringify({id:3,name:'zhaosw'})}-->Json string  contentType:'application/json'  get(string strQuery)  request for object string.need to JsonConvert.DeserializeObject()
 get not to request       request for Array
 get  tips     should add [httpget] when the method name does't start with 'get'

[HttpPost]

Type ajax request Data request contentType api parameter remarks
post primary type-->one parameter:data:{'':1}. tips:not key but value. none  post([fromBody]int id) request for primary type with one param
post priamry type-->multiply parameter:data:JSON.stringify({id:2,name:'zhaosw',age:25}) contentType:'application/json'

post(dynamic obj) 

string name=Convert.toString(obj.name)

request for priamry type with multiply  param
post object type-->one parameter:data:{id:3,name:'zhaosw',age:25} none post(User user). User defined already. request for object type
post object type->one parameter:data:JSON.stringify({id:4,name:'zhaosw'}) contentType:'application/json' post(User user). User defined already. request for object type
post parameter and object type-->one paramter:data:JSON.stringify({no:123,user:{id:1,name;"zhaosw,age:25}}) contentType:'application/json'

post(dynamic obj). 

var no=Convert.toStriong(obj.no)

var user=JsonConvert.DeserializeObject(Convert.ToString(obj.user))

request for complex param
post object type(Array with primary type)-->one parameter:data:JSON.stringify(['1','2','3','4']) contentType:'application/json' post(string[] ids) request for array with primary type
post object type(Array with primary type)-->one parameter:data:JSON.stringify([{id:1,name:'zhao',age:12},{id:2,name:'sw',age:13},{id:3,name:'zhaosw',age:25}]) contentType:'application/json' post(list userlist) request for array with primary type
post

all above request with ajax.

next for end  request for api.

that is webrequest and webresponse more info you can search the internet or below.

     

[HttpPut]

the same with [HttpPost] .refer to [HttpPost]

[HttpDelete]

the same with [HttpPost] .refer to [HttpPost]

重要的链接:

1.webapi 参数传递(上)

2.webapi 参数传递(下)

同时要感谢该作者:赖的安分的博文。

原文地址:https://www.cnblogs.com/zhaosw/p/7481409.html