WebApi 参数绑定方法
WebAPI 2参数绑定方法
简单类型参数
Example 1: Sending a simple parameter in the Url
|
01
02
03
04
05
06
07
08
09
10
11
|
[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
|
01
02
03
04
05
06
07
|
// 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
|
01
02
03
04
05
06
07
|
// 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:
|
01
02
03
04
05
06
|
User-Agent: FiddlerHost: localhost:49407Content-Length: 32Content-Type: application/x-www-form-urlencodedid1=1&id2=2&id3=3 |

Calling the method using Json in the body
|
01
02
03
04
05
06
|
User-Agent: FiddlerHost: localhost:49407Content-Length: 32Content-Type: application/json{ "Id1" : 2, "Id2": 2, "Id3": 3} |

Calling the method using XML in the body
This requires extra code in the Global.asax
|
01
02
03
04
05
06
07
08
09
10
11
12
|
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> |

数组和列表(Array,List)
Example 6: Sending a simple list in the Url
|
01
02
03
04
05
06
07
08
09
10
11
12
|
// 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
|
01
02
03
04
05
06
07
08
09
10
11
12
|
// 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:
|
01
02
03
04
05
06
|
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}] |

Calling with XML:
|
01
02
03
04
05
06
07
08
09
10
|
User-Agent: FiddlerContent-Type: application/xmlHost: localhost:49407Content-Length: 258<ArrayOfParamsObject><ParamsObject><Id1>3</Id1><Id2>76</Id2><Id3>19</Id3></ParamsObject><ParamsObject><Id1>56</Id1><Id2>87</Id2><Id3>94</Id3></ParamsObject><ParamsObject><Id1>976</Id1><Id2>345</Id2><Id3>7554</Id3></ParamsObject></ArrayOfParamsObject> |

Example 8: Sending object lists in the Body
|
01
02
03
04
05
06
07
08
09
10
11
|
[Route("example8")][HttpPost]public string GetListsFromBody([FromBody] List<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.
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
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} ]] |
WebApi 参数绑定方法的更多相关文章
- asp.net webapi 参数绑定总结
首先必须得更正下自己一直以来对于get请求和post请求理解的一个误区:get请求只能通过url传参,post请求只能通过body传参. 其实上面的理解是错误的,翻阅了不少资料及具体实践,正确理解应该 ...
- asp.net webapi参数绑定
content={"content": [{"comb_id": "100323","comb_name": " ...
- WebAPI 2参数绑定方法
简单类型参数 Example 1: Sending a simple parameter in the Url [RoutePrefix("api/values")] public ...
- .net core webapi参数绑定处理
在 Startup的ConfigureServices方法中添加: services.Configure<ApiBehaviorOptions>(options => { optio ...
- springMVC使用HandlerMethodArgumentResolver 自定义解析器实现请求参数绑定方法参数
http://blog.csdn.net/truong/article/details/30971317 http://blog.csdn.net/fytain/article/details/439 ...
- webapi简介及参数绑定
介绍:WebAPI用来开发系统间接口的技术,基于HTTP协议,返回默认是json格式.比wcf简单 更通用,更轻量级,更省流量(json格式):WebAPI尽可能复用MVC路由.ModelBinder ...
- SpringMVC-简单参数绑定
SpringMVC-简单参数绑定 众所周知,springmvc是用来处理页面的一些请求,然后将数据再通过视图返回给用户的,前面的几篇博文中使用的都是静态数据,为了能快速入门springmvc,在 ...
- WebAPI下的如何实现参数绑定
本文将概述在WebAPI方式下将如何将参数绑定到一个action方法,包括参数是如何被读取,一系列规则决定特定环境采用的那种绑定方式,文章最后将给出一些实际的例子. Parameter binding ...
- [译]WebAPI下的如何实现参数绑定
本文将概述在WebAPI方式下将如何将参数绑定到一个action方法,包括参数是如何被读取,一系列规则决定特定环境采用的那种绑定方式,文章最后将给出一些实际的例子. Parameter binding ...
随机推荐
- .Net Core部署到CentOS
本文基于初次或再次尝试部署.Net Core应用到Linux服务器上,我尝试后自我总结的经验一个简单的Demo,尝试部署在Linux服务器上和跨服务器访问数据库. 一.环境介绍 1.本地使用Visua ...
- js面向对象之继承那点事儿根本就不是事
继承 说道这个继承,了解object-oriented的朋友都知道,大多oo语言都有两种,一种是接口继承(只继承方法签名):一种是实现继承(继承实际的方法) 奈何js中没有签名,因而只有实现继承,而且 ...
- JVM的内存分区
JVM的内存分区 这篇文章尝试讨论清楚JVM的内存分区情况. 1. JVM的内存和系统内存的关系 下图是对系统内存及JVM内存的大致描绘 对大多数操作系统,内存可以分为物理内存RAM及Sw ...
- Mysql了解及安装
1.数据库由两部分来构成的 打开一个连接工具,用工具给MySQL发送命令,实际上是给数据库当中的服务下的命令,在服务当中解析命令,最终将命令转化成对物理库上文件IO的操作. 所以数据库的安装位置有两个 ...
- java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.classes.views.index_jsp 问题解决方法
本人使用的是taglib作为模板页,然后碰到的这个问题,如果有类似的可以参考. <%@tag description="Overall Page template" page ...
- 利用QuickCHM制作chm
CHM是一种常见的帮助文件格式,也是电子书的一种格式. 下面是使用QuickCHM制作chm的步骤: 1.先将所有的word文档存储为mht格式,点击,文件--另存为网页,如下 2.确保所有的word ...
- Cannot read property 'component' of undefined 即vue-router 0.x转化为2.x
原文链接:http://blog.csdn.net/m0_37754657/article/details/71269988 由于vue版本为1.0,没有一些vue-router指令:因而需要vue- ...
- 2018-01-08 学习随笔 SpirngBoot整合Mybatis进行主从数据库的动态切换,以及一些数据库层面和分布式事物的解决方案
先大概介绍一下主从数据库是什么?其实就是两个或N个数据库,一个或几个主负责写(当然也可以读),另一个或几个从只负责读.从数据库要记录主数据库的具体url以及BigLOG(二进制日志文件)的参数.原理就 ...
- CentOS安装编译Lua
Lua介绍 Lua 是一个小巧的脚本语言.是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组,由Robert ...
- php+redis 学习 二 悲观锁
<?php header('content-type:text/html;chaeset=utf-8'); /** * redis实战 * * 实现悲观锁机制 * */ $timeout = 5 ...