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 ...
随机推荐
- 在vue中优雅地实现简单页面逆传值
[需求] 要实现的需求很简单,页面从A -> B,用户在B触发操作,将一些数据带回到A页面,在网上找了好久也只看到有人问,但总找不到很好答案.要实现的效果图如下: [联想] 在 ios 开发中, ...
- Protobuf 从入门到实战
简介 从第一次接触Protobuf到实际使用已经有半年多,刚开始可能被它的名字所唬住,其实就它是一种轻便高效的数据格式,平台无关.语言无关.可扩展,可用于通讯协议和数据存储等领域. 优点 平台无关,语 ...
- LCA(最近公共祖先)之倍增算法
概述 对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. 如图,3和5的最近公共祖先是1,5和2的最近公共祖先是4 在本篇中我们先介 ...
- [DeeplearningAI笔记]ML strategy_1_1正交化/单一数字评估指标
机器学习策略 ML strategy 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.1 什么是ML策略 机器学习策略简介 情景模拟 假设你正在训练一个分类器,你的系统已经达到了90%准确 ...
- windows上搭建svn 、 Eclipse上安装svn插件 、 eclipse中如何使用svn
折腾了许久终于搞出来了. svn搭建 一.svn概述 SVN就是用于多个人共同开发同一个项目,共用资源的目的,该文描述了把svn搭建在本地上,和搭建在Eclipse4.5.2上. 二.svn分类 分为 ...
- 使用FileReader实现前端预览所选图片
需求描述 在浏览器环境下进开发网站(也就是B/S架构的应用),获取到当前设备上的图片后,希望可以在上传到服务器前简单预览一下图片内容. 具体实现 主要代码 html 部分 <input type ...
- Nginx负载均衡搭建(Window与Linux)
windows上搭建nginx负载均衡 1.准备几台http服务器软件,这里选用一台apache一台tomcat apache(windows)下载链接:https://www.apachehaus. ...
- Java程序占用的内存可能会大于Xmx
很多人认为Xmx和-Xms参数指定的就是Java程序将会占用的内存,但是这实际上只是Java堆对象将会占用的内存.堆只是影响Java程序占用内存数量的一个因素. 除了堆,影响Java程序所占用内存的因 ...
- MySQL系统临时表、用户临时表
MySQL临时表分为系统使用的临时表和用户使用的临时表. 系统使用的临时表是指MySQL在执行某些SQL语句时需要依赖临时表来完成整个过程.系统使用的临时表的情况可以分为以下几种: * group ...
- [转载]完全版线段树 by notonlysuccess大牛
原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...