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: Fiddler
Host: localhost:49407
Content-Length: 32
Content-Type: application/x-www-form-urlencoded
 
id1=1&id2=2&id3=3

Calling the method using Json in the body

01
02
03
04
05
06
User-Agent: Fiddler
Host: localhost:49407
Content-Length: 32
Content-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: Fiddler
Content-Type: application/xml
Host: localhost:49407
Content-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: Fiddler
Content-Type: application/json
Host: localhost:49407
Content-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: Fiddler
Content-Type: application/xml
Host: localhost:49407
Content-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.1
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:49407
Content-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 参数绑定方法的更多相关文章

  1. asp.net webapi 参数绑定总结

    首先必须得更正下自己一直以来对于get请求和post请求理解的一个误区:get请求只能通过url传参,post请求只能通过body传参. 其实上面的理解是错误的,翻阅了不少资料及具体实践,正确理解应该 ...

  2. asp.net webapi参数绑定

    content={"content": [{"comb_id": "100323","comb_name": " ...

  3. WebAPI 2参数绑定方法

    简单类型参数 Example 1: Sending a simple parameter in the Url [RoutePrefix("api/values")] public ...

  4. .net core webapi参数绑定处理

    在 Startup的ConfigureServices方法中添加: services.Configure<ApiBehaviorOptions>(options => { optio ...

  5. springMVC使用HandlerMethodArgumentResolver 自定义解析器实现请求参数绑定方法参数

    http://blog.csdn.net/truong/article/details/30971317 http://blog.csdn.net/fytain/article/details/439 ...

  6. webapi简介及参数绑定

    介绍:WebAPI用来开发系统间接口的技术,基于HTTP协议,返回默认是json格式.比wcf简单 更通用,更轻量级,更省流量(json格式):WebAPI尽可能复用MVC路由.ModelBinder ...

  7. SpringMVC-简单参数绑定

    SpringMVC-简单参数绑定    众所周知,springmvc是用来处理页面的一些请求,然后将数据再通过视图返回给用户的,前面的几篇博文中使用的都是静态数据,为了能快速入门springmvc,在 ...

  8. WebAPI下的如何实现参数绑定

    本文将概述在WebAPI方式下将如何将参数绑定到一个action方法,包括参数是如何被读取,一系列规则决定特定环境采用的那种绑定方式,文章最后将给出一些实际的例子. Parameter binding ...

  9. [译]WebAPI下的如何实现参数绑定

    本文将概述在WebAPI方式下将如何将参数绑定到一个action方法,包括参数是如何被读取,一系列规则决定特定环境采用的那种绑定方式,文章最后将给出一些实际的例子. Parameter binding ...

随机推荐

  1. 【转】shell学习笔记(二) ——shell变量

    在shell中有3种变量:系统变量,环境变量和用户变量,其中系统变量在对参数判断和命令返回值判断时会使用,环境变量主要是在程序运行时需要设置,用户变量在编程过程中使用量最多. 1 系统变量  变量名 ...

  2. 【转】GPS静态观测网的设计指标

     GPS网的设计指标是指导GPS网设计量化因子,是评价GPS网设计优劣的数值标准.评价GPS网设计的优劣主要从以下三个因素考虑:1.质量(包括精度和可靠性):2.效率:3.费用. 一.GPS网设计的精 ...

  3. java对象引用-要掌握的细节

    hello ,好久没来了. 今天我来和大家分享一下有关引用变量的注意事项,一是加深一下自己的理解,二是对这块不太理解的同学可以看看. 大神可飘过,有什么不对或不足的地方请多多指教,谢谢. 假设场景: ...

  4. win32 dll工程开发创建对话框

    界面编程的CreateWindow函数需要instance,只要获取到dll工程的main的instance参数,就可以使用CreateWindow函数了. 创建对话框需要CreateDialog函数 ...

  5. 创建github仓库的gh-pages分支

    用git symbolic-ref命令将当前工作分支由master切换到一个尚不存在的分支gh-pages. $ git symbolic-ref HEAD refs/heads/gh-pages 删 ...

  6. JAVA泛型使用方法总结

    1. 基本概念: (1)什么是泛型? 泛型,即"参数化类型".即将类型由原来的具体的类型参数化,类似于方法中的变量参数,此时类型也定义成参数形式(可以称之为类型形参),然后在使用或 ...

  7. mysql插入数据时检查是否某字段已存在

    SELECT\n" + " '',\n" + " '{0}',\n" + " '{1}',\n" + " '{2}'\n ...

  8. HBase MetaStore和Compaction剖析

    1.概述 客户端读写数据是先从HBase Master获取RegionServer的元数据信息,比如Region地址信息.在执行数据写操作时,HBase会先写MetaStore,为什么会写到MetaS ...

  9. Spring整合quartz框架实现任务定时调度

    1.  首先需要引入需要的jar包,如上图所示. 2. 编写需要定时调度的测试类: package com.jp.task; import java.util.Date; public class T ...

  10. vue2.0父子组件之间通信

    父组件是通过props属性给子组件通信的来看下代码: 父组件: <parent> <child :child-com="content"></chil ...