zoukankan      html  css  js  c++  java
  • 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 参数传递(下)

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

  • 相关阅读:
    VS生成事件时复制文件到指定位置
    Windows服务BAT命令-安装、卸载、启动、停止
    Lambda表达式动态组装查询条件
    动软模板使用流程
    使用jquery.qrcode生成二维码
    MySQL报错:Packets larger than max_allowed_packet are not all
    Excel操作之VLOOKUP函数
    Visual Studio 编辑器打开项目后,一直提醒Vs在忙,解决方法
    使用BaiDu Java Script Web Api 在Web开发中嵌入地图使用步骤
    C#或Net连接Oracle操作提示 Oracle 客户端 version 8.1.7 或更高版本报错
  • 原文地址:https://www.cnblogs.com/zhaosw/p/7481409.html
Copyright © 2011-2022 走看看