1.FromUri使用

将数据通过url方式传递。我们需要在webapi方法标明,这个参数只接受url中参数的值,

 $("#Save").click(function () {
$.ajax({
url: 'http://localhost:21162/api/person/?str1=1&str2=3',
type: 'Get',
dataType: 'json',
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});

在参数比较少情况下、或者Url长度小于256的情况下。可以考虑使用FromUri, 部分游览器会现在url大小,在这种情况下我们就要使用FromBody

2.FromBody

将数据通过post data方式方式传递。我们需要在webapi方法标明接受data参数,传递json参数


        $("#Save").click(function () {
$.ajax({
url: 'http://localhost:21162/api/person/',
type: 'Get',
dataType: 'json',
data: { "str1": "1", "str2": "2" },
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});

3.ActionName使用,通过制定Action来调用方法

        [ActionName("GetAllPerson")]
public IEnumerable<Person> GetAllPerson([FromUri] string str1, [FromUri] string str2)
{
return new List<Person>() {
new Person() {ID="1", Name="李鸿章ALL",Age=15},
new Person() {ID="2", Name="杨玉红ALL",Age=15} };
}
      $("#Save").click(function () {
$.ajax({
url: 'http://localhost:21162/api/person/GetAllPerson?str1=1&str2=3',
type: 'Get',
dataType: 'json',
//data: { "str1": "1", "str2": "2" },
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});

4.NonAction  表示不被外部应用程序调用

5. [AcceptVerbs("GET","POST")]

        [ActionName("GetAllPerson")]  //指定ActionName
[NonAction] //不被外部应用程序调用
[AcceptVerbs("GET","POST")] //表示既可以Get请求,又可以Post请求
public IEnumerable<Person> GetAllPerson([FromUri] string str1, [FromUri] string str2)
{
return new List<Person>() {
new Person() {ID="1", Name="李鸿章ALL",Age=15},
new Person() {ID="2", Name="杨玉红ALL",Age=15} };
}

理解中WebAPI的属性和相关操作 FormBody和 FormUri等(WebAPI 二)的更多相关文章

  1. Web API与AJAX:理解FormBody和 FormUri的WebAPI中的属性

    这是这一系列文章"与 AJAX 的 Web API".在这一系列我们都解释消耗 Web API rest 风格的服务使用 jQuery ajax() 和其他方法的各种方法.您可以阅 ...

  2. python三大框架之一flask中cookie和session的相关操作

    状态保持 Cookie cookie 是指某些网站为了 辨别  用户身份,进行会话跟踪而储存在用户本地的数据(通常会经过加密),复数形式是 coolies. cookie是由服务器端生成,发送给客户端 ...

  3. 11G RAC 中 OCR 及Voting Disk 相关操作

    一.启动oracle clusterware先决条件:Oracle High Availability Services daemon(OHASD)运行在所有集群节点上1.启动整个Oracle Clu ...

  4. java-Eclipse中使用JDBC连接数据库及相关操作

    准备工作:mysql-connector-java-5.1.6-bin.jar配置 package com.job; import java.sql.Connection; import java.s ...

  5. Python3中的List列表的相关操作

    列表对象内建函数 1. append(obj) 在列表的末尾添加新元素obj.例: >>> a = ['a', 'b', 'c'] >>> a.append('d' ...

  6. JS中对象的定义及相关操作

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. set-cookie中的SameSite属性

    原文:set-cookie中的SameSite属性 再见,CSRF:讲解set-cookie中的SameSite属性 2016-04-14 13:18:42 来源:360安全播报 作者:暗羽喵 阅读: ...

  8. C# 对类中的保护成员进行写操作(邀请大家拍砖)

    假如我有一个类库 Lib,提供一个类 ClassA 对外服务,ClassA 中有若干只读属性 PropA, PropB 等等,外部调用者无法对 ClassA 中的 PropA 和 PropB 进行写操 ...

  9. 理解特性attribute 和 属性property的区别 及相关DOM操作总结

    查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点:  ...

随机推荐

  1. 【solr专题之四】在Tomcat 中部署Solr4.x

    1.安装Tomcat (1)下载并解压至/opt/tomcat中 # cd /opt/jediael # tar -zxvf apache-tomcat-7.0.54.tar.gz # mv apac ...

  2. 使用nextInt()等接受输入时必须注意换行符的输入

    参考http://stackoverflow.com/questions/19331426/for-loop-does-not-iterate-the-way-i-want 见以下代码: packag ...

  3. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

  4. [TYVJ] P1049 最长不下降子序列

    最长不下降子序列 描述 Description 求最长不下降子序列的长度   输入格式 InputFormat 第一行为n,表示n个数第二行n个数   输出格式 OutputFormat 最长不下降子 ...

  5. StreamReader与StreamWriter

    StreamReader实现了抽象基类TextReader类,而StreamWriter实现了抽象基类TextWriter.分别用于对流的读取与写入. 先从StreamReader说起 一.构造方法 ...

  6. UNIX网络编程---TCP客户/服务器程序示例(五)

    一.概述 客户从标准输入读入一行文本,并写给服务器 服务器从网络输入读入这行文本,并回射给客户 客户从网络输入读入这行回射文本,并显示在标准输出上 二.TCP回射服务器程序:main函数 这里给了函数 ...

  7. UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>

    D - 基爷的中位数 Time Limit: 5000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  8. 【UVA 11997 K Smallest Sums】优先级队列

    来自<训练指南>优先级队列的例题. 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意:给定 ...

  9. postgresql基本语句

    preface,熟悉pgsql sql Language article disorder; 1,pgsql数据库控制台Cli(command line interface) help mannual ...

  10. android中通过自定义xml实现你需要的shape效果 xml属性配置

    在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片,然后在写一个selector的 ...