最近学习了下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. NYOJ--520

    最大素因子 原题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=520 分析:先筛素数,同时记录下素数的序号,然后质因数分解. #include ...

  2. grep index.php *

    zb@zb-computer:/usr/local/nginx/conf/vhost$ grep index.php * caomall17.conf: index index.html index. ...

  3. nginx 初探 之反向代理

    首先要解释的是什么叫做反向代理? 平时我们浏览网页可以输入网址直接访问,  但如果访问国外的网站,  可能就没那么简单('中国特色'),  这时候我们需要配置一个代理服务器, 然后通过此服务器中转来访 ...

  4. 2017北京国庆刷题Day2 afternoon

    期望得分:100+100+50=250 实际得分:100+70+50=220 T1 最大值(max) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一 ...

  5. NOIP 2014 提高组 Day1

    期望得分:100+100+50=250 实际得分:100+100+50=250 此次NOIP  ZJ省一分数线:500,SD:345 https://www.luogu.org/problem/lis ...

  6. 基础但是很重要的2-sat POJ 3678

    http://poj.org/problem?id=3678 题目大意:就是给你n个点,m条边,每个点都可以取值为0或者1,边上都会有一个符号op(op=xor or and三种)和一个权值c.然后问 ...

  7. ZOJ 3777 B - Problem Arrangement 状压DP

    LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:有N(\( N <= 12 \))道题,排顺序 ...

  8. [php]php错误处理机制

    1.判断文件是否存在,file_exists("文件名") or die("no such file");2.set_error_hanlder("错 ...

  9. UIDynamicBehavior的行为类翻译

    CHENYILONG Blog UIDynamicBehavior的行为类翻译 © chenyilong. Powered by Postach.io Blog

  10. U盘出现大量乱码文件,并且不能彻底删除

    问题如图所示: 问题出现原因:不正常的插拔等情况造成的,导致U盘的文件分配表错乱了 解决方法:参考http://bbs.cfanclub.net/thread-405004-1-1.html 运行ch ...