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 ...
随机推荐
- 【转】Linux上vi(vim)编辑器使用教程
Linux上vi(vim)编辑器使用教程 ------------------------------------------------------------ ikong ------------ ...
- ELK日志管理搭建
目录: 一.介绍 二.安装JDK 三.安装Elasticsearch 四.安装Kibana 五.安装Nginx 六.安装Logstash 七.安装Logstash-forwarder 八.测试 系统环 ...
- AppScan 扫描测试策略
使用 AppScan 进行扫描 针对大型网站的扫描,我们按照戴明环 PDCA 的方法论来进行规划和讨论,建议 AppScan 使用步骤:计划(Plan).执行(Do).检查(check).分析(Ana ...
- iOS-UINavigationBar【颜色设置】
UINavigationBar的一些颜色设置,以前老是忘,这次记住了 - (void)setNavigationBar{ ///NavigationBar backgroundcolor[背景色] [ ...
- xBIM 格式之间转换
目录 xBIM 应用与学习 (一) xBIM 应用与学习 (二) xBIM 基本的模型操作 xBIM 日志操作 XBIM 3D 墙壁案例 xBIM 格式之间转换 xBIM 使用Linq 来优化查询 x ...
- BZOJ 3781: 小B的询问 [莫队]
求区间每种颜色出现次数平方和 写裸题练手 #include <iostream> #include <cstdio> #include <algorithm> #i ...
- BZOJ 4767: 两双手 [DP 组合数]
传送门 题意: 给你平面上两个向量,走到指定点,一些点不能经过,求方案数 煞笔提一开始被题面带偏了一直郁闷为什么方案不是无限 现在精简的题意.....不就是$bzoj3782$原题嘛,还不需要$Luc ...
- E 洛谷 P3598 Koishi Loves Number Theory[数论]
题目描述 Koishi十分喜欢数论. 她的朋友Flandre为了检测她和数论是不是真爱,给了她一个问题. 已知 给定和个数,求对取模. 按照套路,呆萌的Koishi当然假装不会做了,于是她来向你请教这 ...
- 基于爬取百合网的数据,用matplotlib生成图表
爬取百合网的数据链接:http://www.cnblogs.com/YuWeiXiF/p/8439552.html 总共爬了22779条数据.第一次接触matplotlib库,以下代码参考了matpl ...
- MIT-线性代数笔记(1-6)
学习目录 第 01 讲 行图像和列图像 第 02 讲 矩阵消元 第 03 讲 矩阵的乘法和逆矩阵 第 04 讲 矩阵的LU 分解 第 05 讲 转置.置换和空间 第 06 讲 列空间和零空间 第 07 ...