最近学习了下WebApi,WebApi是RESTful风格,根据请求方式决定操作。以博客的形式写出来,加深印象以及方便以后查看和复习。

1、首先我们使用VS创建一个空的WebApi项目

2、新建实体以及控制器类

     public class Product
{
public int Id { set; get; }
public string Name { set; get; }
public string Description { set; get; }
}
     public class HomeController : ApiController
{
static List<Product> modelList = new List<Product>()
{
new Product(){Id=,Name="电脑",Description="电器"},
new Product(){Id=,Name="冰箱",Description="电器"},
}; //获取所有数据
[HttpGet]
public List<Product> GetAll()
{
return modelList;
} //获取一条数据
[HttpGet]
public Product GetOne(int id)
{
return modelList.FirstOrDefault(p => p.Id == id);
} //新增
[HttpPost]
public bool PostNew(Product model)
{
modelList.Add(model);
return true;
} //删除
[HttpDelete]
public bool Delete(int id)
{
return modelList.Remove(modelList.Find(p => p.Id == id));
} //更新
[HttpPut]
public bool PutOne(Product model)
{
Product editModel = modelList.Find(p => p.Id == model.Id);
editModel.Name = model.Name;
editModel.Description = model.Description;
return true;
}
}

3、新建html页面使用ajax操作

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>
<script src="/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function () {
add();
update();
find();
remove();
getAll();
});
function getAll() {
$.ajax({
url: "api/Home/",
type: 'GET',
success: function (data) {
console.log(data);
}
});
} function find() {
$.ajax({
url: "api/Home/1",
type: 'GET',
success: function (data) {
console.log(data);
}
});
} function add() {
$.ajax({
url: "api/Home/",
type: "POST",
data: { "Id": "3", "Name": "电磁炉", "Description": "电器" },
success: function (data) {
console.log(data);
}
}); } function update() {
$.ajax({
url: "api/Home/",
type: 'PUT',
data: { "id": "1", "Name": "洗衣机", "Description": "家具" },
success: function (data) {
console.log(data);
}
});
} function remove() {
$.ajax({
url: "api/Home/1",
type: 'DELETE',
success: function (data) {
console.log(data);
}
});
}
</script>
</head>
<body>
<h1>WebApi基本操作</h1>
</body>
</html>

4、通过开发人员工具可以看到如下

WebApi默认是以XML格式返回,但是一般我们需要返回Json,通过在Global.asax里的Application_Start方法中加上如下代码可以移除XML,让其只返回Json

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

有一个需要注意的是,如果在参数写的是实体类,那么需要加上相应的特性(post、put请求不需要),如下

         [HttpGet]
public User Get([FromUri]User user)
{
return user;
}

Web APi入门之基本操作(一)的更多相关文章

  1. 转载-Web API 入门

    An Introduction to ASP.NET Web API 目前感觉最好的Web API入门教程 HTTP状态码 Web API 强势入门指南 Install Mongodb Getting ...

  2. Web API 入门指南 - 闲话安全

    Web API入门指南有些朋友回复问了些安全方面的问题,安全方面可以写的东西实在太多了,这里尽量围绕着Web API的安全性来展开,介绍一些安全的基本概念,常见安全隐患.相关的防御技巧以及Web AP ...

  3. Web API入门指南(安全)转

    安全检测的工具站点:https://www.owasp.org/index.php/Category:Vulnerability_Scanning_Tools Web API入门指南有些朋友回复问了些 ...

  4. 【ASP.NET Web API教程】1 ASP.NET Web API入门

    原文 [ASP.NET Web API教程]1 ASP.NET Web API入门 Getting Started with ASP.NET Web API第1章 ASP.NET Web API入门 ...

  5. Web API 入门指南

    Web API 入门指南 - 闲话安全2013-09-21 18:56 by 微软互联网开发支持, 231 阅读, 3 评论, 收藏, 编辑 Web API入门指南有些朋友回复问了些安全方面的问题,安 ...

  6. Web API 入门 二 媒体类型

    还是拿上面 那篇 Web API 入门 一  的那个来讲 在product类中加一个时间属性

  7. (转)Web API 入门指南 - 闲话安全

    原文地址:http://www.cnblogs.com/developersupport/p/WebAPI-Security.html Web API入门指南有些朋友回复问了些安全方面的问题,安全方面 ...

  8. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  9. Web APi入门之移除XML格式(一)

    前言 回头想来,没想到自己却坚持下来了,EntityFramework系列终于全部完成了,给自己点个赞先.本系列将着手于Web API,关于一些基础的介绍及定义就不再叙述,请参考园友们文章,非常详细, ...

随机推荐

  1. hihocoder 1509异或排序

    描述 给定一个长度为 n 的非负整数序列 a[1..n] 你需要求有多少个非负整数 S 满足以下两个条件: (1).0 ≤ S < 2^60 (2).对于所有 1 ≤ i < n ,有 ( ...

  2. 2018-2019 ACM-ICPC 焦作赛区 部分题解

    题目链接:https://codeforces.com/gym/102028 B. Ultraman vs. Aodzilla and Bodzilla 题意: 两只怪兽,它们的生命和攻击分别为hpA ...

  3. 「PLC」PLC的硬件与工作原理

  4. P4752 Divided Prime

    P4752 Divided Prime 题目描述 给定一个数字 AA ,这个 AA 由 a_1,a_2,\cdots,a_Na 1 ​ ,a 2 ​ ,⋯,a N ​ 相乘得到. 给定一个数字 BB ...

  5. Google Map API使用详解(一)——Google Map开发背景知识

    一.谷歌地图主页 谷歌地图对应不同的地区都会有一些专门的主页,首次登陆时会显示这些地区.比如,香港的:http://maps.google.com.hk,台湾的:http://maps.google. ...

  6. errno错误号含义

    errno0 : Success errno1 : Operation not permitted errno2 : No such file or directory errno3 : No suc ...

  7. bluebird -1 New Promise方法

    new Promise new Promise(function(function resolve, function reject) resolver) -> Promise 创建一个Prom ...

  8. 《JavaScript 实战》:Tween 算法及缓动效果

    Flash 做动画时会用到 Tween 类,利用它可以做很多动画效果,例如缓动.弹簧等等.我这里要教大家的是怎么利用 Flash 的 Tween 类的算法,来做js的Tween算法,并利用它做一些简单 ...

  9. [php]几个常用函数

    count(arr);用于统计数组的元素个数 is_array(arr);判断给定变量是不是数组 var_dump(var||arr);打印数组或变量信息(类型和值): print_r(var||ar ...

  10. maven使用过程中遇到的问题总汇

    1:web.xml is missing and <failOnMissingWebXml> is set to true 造成原因: 使用maven创建项目时有时在pom.xml的war ...