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. Android 调试机制

    Android的调试信息可以根据DDMS进行查看,Logcat日志输出所有的调试信息,为了方便的找到我们需要的打印信息,可以在logcat后面增加过滤器.比如你想查看system.out.printl ...

  2. MapReduce读取hdfs上文件,建立词频的倒排索引到Hbase

    Hdfs上的数据文件为T0,T1,T2(无后缀): T0: What has come into being in him was life, and the life was the light o ...

  3. UVa 400 Unix Is

    题意:给出n个字符串,按照字典序排列,再按照规则输出. ===学习的紫书,题目意思很清楚,求列数和行数最开始看的时候木有看懂啊啊啊 列数:即为(60-M)/(M+2)+1;即为先将最后那一列减去,算普 ...

  4. Need to add a caption to a jpg, python can't find the image

    importImage,ImageDraw,ImageFont, os, glob list = glob.glob('*.jpg')for infile in list:print infile f ...

  5. MKNetworkKit: 网络处理又一利器

    没有认识MK之前,即便ASI已经不再更新,也没有启用ASI.因为ASI对于网络的处理更偏向于底层,适合针对各种情形的扩展. 但是,今天我要开始使用 MKNetworkKit了,项目在github上,使 ...

  6. BZOJ 2429 聪明的猴子

    kruskal. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...

  7. [反汇编练习] 160个CrackMe之022

    [反汇编练习] 160个CrackMe之022. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  8. 学习opengl(起步)

    库可以在这里下载 第一个程序: #ifndef GLUT_DISABLE_ATEXIT_HACK #define GLUT_DISABLE_ATEXIT_HACK #endif #include &l ...

  9. Centos6.5自带mysql的启动

    CentOS6.5选择web server版本,安装完以后,用rpm -qa | grep mysql 发现已经安装, 但是使用service mysqld start 显示mysqld命令不存在,后 ...

  10. AIX 第2章 指令记录

    root@db:/#mount  node       mounted        mounted over    vfs       date        options     ------- ...