c# webapi的参数传递方式:1、查询字符串(query string);2、内容主体(content body)

当然也有cookie或url部分或头部信息(header)等其它传方式,这里仅讨论这两种。

1、查询字符串(query string)

如果是简单数据类型或使用了FromUri特性修饰的参数,将使用查询字符串作为数据源。

例1:因为参数p为复合类型,默认使用request body做为数据源,所以如果想以query string为数据源,则不能省略 FromUri修饰。

     /// <summary>
/// <example>
/// 调用方式:
/// test?p=1&p=2
/// test?p[]=1,2
/// </example>
/// </summary>
public class Test : ApiController
{
public string Post([FromUri]string[] p)
{
if (p == null)
return "NULL";
else if (p.Length == )
return "EMPTY";
else return string.Join(",", p);
}
}

例2:

     /// <summary>
/// <example>
/// 调用方式:
/// test?p=1
/// </example>
/// </summary>
public class Test : ApiController
{
public string Post(string p)
{
if (p == null)
return "NULL";
else if (p.Length == )
return "EMPTY";
else return string.Join(",", p);
}
}

2、内容主体(content body)

如果是复合数据类型或使用了FromBody特性修饰的参数,将使用内容主体作为数据源。在webapi中,body不会被缓存,仅可获取一次,故只能对body接收一次参数,所以不适合传递多参数数据。

例1:x-www-form-urlencoded编码方式时,注意不能带参数名。

     /// <summary>
/// <example>
/// post请求头:
/// Content-Type: application/x-www-form-urlencoded
/// Cache-Control: no-cache
///
/// =v1&=v2
/// </example>
/// </summary>
public class TestController : ApiController
{
public string Post([FromBody]string[] p)
{
if (p == null)
return "NULL";
else if (p.Length == )
return "EMPTY";
else return string.Join(",", p);
}
}

例2:Content-Type为json时

     /// <summary>
/// <example>
/// post请求头:
/// Content-Type: application/json
/// Cache-Control: no-cache
///
/// ["v1","v2"]
/// </example>
/// </summary>
public class TestController : ApiController
{
public string Post([FromBody]string[] p)
{
if (p == null)
return "NULL";
else if (p.Length == )
return "EMPTY";
else return string.Join(",", p);
}
}

WebApi参数传递的更多相关文章

  1. webapi 参数传递详解

    原因 经常有朋友遇到webapi参数传递问题,自己也碰到过一些坑,在此记录下正确的姿势,简单参数传递相信没有人会有问题,容易出现问题的是对象参数和表单参数. 1.WebApi5.2.3有FromBod ...

  2. [转]webApi 参数传递总结

    在WebAPI中,请求主体(HttpContent)只能被读取一次,不被缓存,只能向前读取的流. 举例子说明: 1. 请求地址:/?id=123&name=bob 服务端方法: void Ac ...

  3. WebApi参数传递总结

    在WebAPI中,请求主体(HttpContent)只能被读取一次,不被缓存,只能向前读取的流. 举例子说明: 1. 请求地址:/?id=123&name=bob 服务端方法: void Ac ...

  4. WebApi参数传递实例

    Get 1.基础数据类型 1.1方法只含有一个形参 (1)Get传值的本质是通过url字符串拼接(2)Get传递参数本质是url字符串拼接,Request-Head头部传递,Request-Body中 ...

  5. WebApi参数传递总结(转)

    出处:http://www.cnblogs.com/Juvy/p/3903974.html 在WebAPI中,请求主体(HttpContent)只能被读取一次,不被缓存,只能向前读取的流. 举例子说明 ...

  6. WebApi中的参数传递

    在WebApi开发过程中,遇到一些客户端参数格式传输错误,经常被问到参数如何传递的一些问题,因此就用这篇博客做一下总结,肯定其它地方呢也有类似的一些文章,但是我还是喜欢通过这种方式将自己的理解记录下来 ...

  7. 【WebApi系列】详解WebApi如何传递参数

    WebApi系列文章 [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi参数的传递 [04]详解WebApi测试和PostMan [05]浅谈W ...

  8. WebApi如何传递参数

    一 概述 一般地,我们在研究一个问题时,常规的思路是为该问题建模:我们在研究相似问题时,常规思路是找出这些问题的共性和异性.基于该思路,我们如何研究WebApi参数传递问题呢? 首先,从参数本身来说, ...

  9. webapi-2 接口参数

    1. 实例 using System; using System.Collections.Generic; using System.Linq; using System.Net; using Sys ...

随机推荐

  1. sqlserver资源下载

    安装包可以从itellyou下载 NorthWind 安装SQL2000SampleDb.msi 之后可以在C:\SQL Server 2000 Sample Databases目录 2016/01/ ...

  2. leetcode:Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  3. 《OD学算法》排序

    参考 http://www.cnblogs.com/kkun/archive/2011/11/23/2260312.html http://blog.csdn.net/wuxinyicomeon/ar ...

  4. Hadoop集群(第9期)_MapReduce初级案例

    1.数据去重  "数据去重"主要是为了掌握和利用并行化思想来对数据进行有意义的筛选.统计大数据集上的数据种类个数.从网站日志中计算访问地等这些看似庞杂的任务都会涉及数据去重.下面就 ...

  5. 浅谈javascript中的作用域

    首先说明一下:Js中的作用域不同于其他语言的作用域,要特别注意     JS中作用域的概念: 表示变量或函数起作用的区域,指代了它们在什么样的上下文中执行,亦即上下文执行环境.Javascript的作 ...

  6. 内存分配(c/c++)

    C++中内存分配          内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储区. 1,栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清除的变量的存储区.里面的 ...

  7. HDU 1711 Number Sequence (数字KMP,变形)

    题意: 在一个序列中找到一个连续的子序列,返回其开始位置. 思路: 每个数字当成1个字符,长的序列是原串,短的序列是模式串,求next数组后进行匹配. #include <iostream> ...

  8. 【C#学习笔记】保存文件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. 【英语】Bingo口语笔记(10) - 常见词汇的缩读

  10. yii2.0 url 跳转

    //转发 $this->render('page1',['id'=>3,'mark'=>2]);    //显示page1页面 并传递 id mark 2个参数 //重定向 $thi ...