Web Api:基于RESTful标准
参考链接:http://www.cnblogs.com/lori/p/3555737.html
简单的了解到RESTful架构后,跟着以上链接做了一个小练习。
Step1:
新建WebApi项目,新建controller命名为TestController
在TestController中的代码如下:
public class TestController : ApiController
{
/// <summary>
/// User Data List
/// </summary> private readonly List<Users> _userList = new List<Users>
{
new Users{ UserID=,UserName="Admin",UserEmail="bfyxzls@sina.com"},
new Users {UserID = , UserName = "Spiderman", UserEmail = "Spiderman@cnblogs.com"},
new Users {UserID = , UserName = "Batman", UserEmail = "Batman@cnblogs.com"}
}; /// <summary>
/// 得到列表对象
/// </summary>
/// <returns></returns> public IEnumerable<Users> Get()
{
return _userList;
} /// <summary>
/// 得到一个实体,根据主键
/// </summary>
/// <param name="id"></param>
/// <returns></returns> public Users Get(int id)
{
return _userList.FirstOrDefault(i => i.UserID == id);
} /// <summary>
/// 添加
/// </summary>
/// <param name="form">表单对象,它是唯一的</param>
/// <returns></returns> public Users Post([FromBody] Users entity)
{
_userList.Add(entity);
return entity;
} /// <summary>
/// 更新
/// </summary>
/// <param name="id">主键</param>
/// <param name="form">表单对象,它是唯一的</param>
/// <returns></returns> public Users Put(int id, [FromBody] Users entity)
{
var user = _userList.FirstOrDefault(i => i.UserID == id);
if (user!=null)
{
user.UserName = entity.UserName;
user.UserEmail = entity.UserEmail;
}
return user;
} /// <summary>
/// 删除
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns> public void Delete(int id)
{
_userList.Remove(_userList.FirstOrDefault(i => i.UserID == id));
} }
Step2:
新建html页面,在html中代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../../Scripts/jquery-1.10.2.min.js"></script>
<script src="../../Scripts/jquery-1.10.2.js"></script>
</head>
<body>
<fieldset>
<legend>
测试Web Api
</legend>
<a href="javascript:add()">添加(post)</a>
<a href="javascript:update(1)">更新(put)</a>
<a href="javascript:deletes(1)">删除(delete)</a>
<a href="/api/test">列表(Get)</a>
<a href="/api/test/1">实体(Get)</a>
</fieldset>
<script>
function add() {
$.ajax({
url: "/api/Test/",
type: "POST",
data: { "UserID": , "UserName": "test", "UserEmail": "Parry@cnblogs.com" },
success: function (data) { alert(JSON.stringify(data)); }
});
} //更新
function update(id) {
$.ajax({
url: "/api/Test?id=" + id,
type: "Put",
data: { "UserID": , "UserName": "moditest", "UserEmail": "Parry@cnblogs.com" },
success: function (data) { alert(JSON.stringify(data)); }
});
}
function deletes(id) {
$.ajax({
url: "/api/Test/1",
type: "DELETE",
success: function (data) { alert(data); }
});
}
</script>
</body>
</html>
总结:
从练习中学习到RESTful标准相比较传统的webService方式,在前端提交到后台服务上,更加方便快捷,并且以上练习,让人很清晰可以根据以前掌握的ajax提交,只是改变了URL和type方式,其它的方式都更加亲切,所以更加的通俗易懂。
Web Api:基于RESTful标准的更多相关文章
- WebApi系列~基于RESTful标准的Web Api
微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...
- 基于RESTful标准的Web Api
微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...
- WebApi系列~基于RESTful标准的Web Api 转载 https://www.cnblogs.com/lori/p/3555737.html
微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...
- ASP.NET Core Web API 开发-RESTful API实现
ASP.NET Core Web API 开发-RESTful API实现 REST 介绍: 符合REST设计风格的Web API称为RESTful API. 具象状态传输(英文:Representa ...
- ASP.NET Web API基于OData的增删改查,以及处理实体间关系
本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先是比较典型的一对多关系,Supplier和Product. public class Product { ...
- [转]ASP.NET Web API基于OData的增删改查,以及处理实体间关系
本文转自:http://www.cnblogs.com/darrenji/p/4926334.html 本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先 ...
- 新建 ASP.NET Core Web API 项目 -- RESTFul 风格 Hello World!
一.创建一个空项目 请查看 新建 .NET Core 项目 -- Hello World! 一节,新建一个项目: 二.添加引用并修改配置为 Web API (.NET Core 已将 MVC/W ...
- asp.net core 2.0 web api基于JWT自定义策略授权
JWT(json web token)是一种基于json的身份验证机制,流程如下: 通过登录,来获取Token,再在之后每次请求的Header中追加Authorization为Token的凭据,服务端 ...
- Web Api 基于Zookeeper的服务注册与发现
安装与差异 Zookeeper安装请参考我上篇文章 http://www.cnblogs.com/woxpp/p/7700368.html 基于Nginx的服务提供和消费 基于zookeeper的服务 ...
随机推荐
- bzoj4448 情报传递
题目链接 离线+树上主席树,主席树维护时间标记 注意查询时如果c<0要把c赋为0: #include<iostream> #include<cstdio> #includ ...
- MyEclipse如何修改XML文件默认打开的编辑器
1.MyEclipse如何修改XML文件默认打开的编辑器 Windows--->Preferences--->General--->Editors--->File Associ ...
- Java中使用自定义类封装数组,添加类方法实现数据操作
1.具体见注释 2.后续或有更新 public class MyArray { private long[] array; private int cnt; // 自定义数组类的元素个数 /** 使用 ...
- m3u8文件下载合并的一种方法
# -*- coding: utf-8 -*- """ Created on Wed Mar 14 15:09:14 2018 @author: Y "&quo ...
- js 解密 16进制转10进制,再取ascii码的对应值
如:\x64 对应 16进制 0x64 转10进制就是 0x64.toString(10) == 100, 查对应的ascii码表得到 ‘d' <div id=code style='displ ...
- XX-net
环境:win10企业版 #停用“ip helper”服务 net stop "ip helper" #启用“ip helper”服务 net start "ip help ...
- P2048 [NOI2010]超级钢琴(RMQ+堆+贪心)
P2048 [NOI2010]超级钢琴 区间和--->前缀和做差 多次查询区间和最大--->前缀和RMQ 每次取出最大的区间和--->堆 于是我们设个3元组$(o,l,r)$,表示左 ...
- oracle 12.2 linux/solaris正式发布
oracle 12.2 linux/solaris正式发布,可以从http://www.oracle.com/technetwork/database/enterprise-edition/downl ...
- easyui-datagrid合并相同行功能扩展
//合并相同行$.extend($.fn.datagrid.methods, { autoMergeCells: function (jq, fields) { return jq.each(func ...
- Restful framework【第六篇】认证组件
基本用法 -认证功能 1 写一个类,继承BaseAuthentication 2 def authenticate(self,request) ,记住传request对象 -如果验证通过,返回None ...