理解中WebAPI的属性和相关操作 FormBody和 FormUri等(WebAPI 二)
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 二)的更多相关文章
- Web API与AJAX:理解FormBody和 FormUri的WebAPI中的属性
这是这一系列文章"与 AJAX 的 Web API".在这一系列我们都解释消耗 Web API rest 风格的服务使用 jQuery ajax() 和其他方法的各种方法.您可以阅 ...
- python三大框架之一flask中cookie和session的相关操作
状态保持 Cookie cookie 是指某些网站为了 辨别 用户身份,进行会话跟踪而储存在用户本地的数据(通常会经过加密),复数形式是 coolies. cookie是由服务器端生成,发送给客户端 ...
- 11G RAC 中 OCR 及Voting Disk 相关操作
一.启动oracle clusterware先决条件:Oracle High Availability Services daemon(OHASD)运行在所有集群节点上1.启动整个Oracle Clu ...
- java-Eclipse中使用JDBC连接数据库及相关操作
准备工作:mysql-connector-java-5.1.6-bin.jar配置 package com.job; import java.sql.Connection; import java.s ...
- Python3中的List列表的相关操作
列表对象内建函数 1. append(obj) 在列表的末尾添加新元素obj.例: >>> a = ['a', 'b', 'c'] >>> a.append('d' ...
- JS中对象的定义及相关操作
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- set-cookie中的SameSite属性
原文:set-cookie中的SameSite属性 再见,CSRF:讲解set-cookie中的SameSite属性 2016-04-14 13:18:42 来源:360安全播报 作者:暗羽喵 阅读: ...
- C# 对类中的保护成员进行写操作(邀请大家拍砖)
假如我有一个类库 Lib,提供一个类 ClassA 对外服务,ClassA 中有若干只读属性 PropA, PropB 等等,外部调用者无法对 ClassA 中的 PropA 和 PropB 进行写操 ...
- 理解特性attribute 和 属性property的区别 及相关DOM操作总结
查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点: ...
随机推荐
- linux下virtualenv的python版本
virtualenv是python开发中一个重要的工具,它可以帮助我们创建一个干净的python解释环境,创建虚拟环境时,这个虚拟环境的 python版本往往是系统默认的2.x版本.别急,我们只需要一 ...
- css布局学习笔记之position属性
position属性用于定位元素,它的几个值分别如下: 1,static static 是默认值.任意 position: static; 的元素不会被特殊的定位.一个 static 元素表示它不会被 ...
- PHP使用curl伪造IP地址和header信息
curl虽然功能强大,但是只能伪造$_SERVER["HTTP_X_FORWARDED_FOR"],对于大多数IP地址检测程序来说,$_SERVER["REMOTE_AD ...
- 在CentOS 7下试验Drupal 7
按顺序安装好Apache.MariaDB和PHP,启动Apache和MariaDB,创建一个UTF-8字符集的数据库. > create database if not exists drupa ...
- php在cli和cgi方式下获取服务器ip的实例
php cli方式下获取服务器ip php内核没有提供这样的命令,需要借助linux中的命令ifconfig来实现,如下两个函数代码: function getServerIp(){ ...
- Pet(hdu 4707 BFS)
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Qt中事件处理的方法(三种处理方法,四种覆盖event函数,notify函数,event过滤,事件处理器。然后继续传递给父窗口。可观察QWidget::event的源码,它是虚拟保护函数,可改写)
一.Qt中事件处理的方式 1.事件处理模式一 首先是事件源产生事件,最后是事件处理器对这些事件进行处理.然而也许大家会问, Qt中有这么多类的事件,我们怎么样比较简便的处理每个事件呢?设想,如果是 ...
- 软件测试-nextDate问题
NextDate 函数包含三个变量:month . day 和 year ,函数的输出为输入日期后一天的日期. 例如,输入为 2006年3月 7日,则函数的输出为 2006年3月8日 .要求输入变量 ...
- 转:Dynamic Binding Of RDLC To ReportViewer
Introduction I was struggling to find the solution to bind rdlc dynamically to reportviewer .We had ...
- H.264 RTP 封包格式
H.264 视频 RTP 负载格式 1. 网络抽象层单元类型 (NALU) NALU 头由一个字节组成, 它的语法如下: +---------------+ |0|1|2|3|4|5|6|7 ...