最近学习了下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. ROS中的CMakeLists.txt (转)

    在ROS的编程过程中,如果CMakeLists.txt如果写不好,编译就很难成功.如果看不懂CMakeLists.txt那么很多错误你也不知道时什么回事.所以深入了解它是很右必要的.现在我们就来看看它 ...

  2. ios错误码:NSError对象.code

    1. URL Loading System Error Codes These values are returned as the error code property of an NSError ...

  3. laravel5.1 使用中间表的多对多关联

    用户表user 标签表tag 中间表user_tag(user_id,tag_id) 在user模型中定义tags关联如下: public function tags() { return $this ...

  4. CSUST 四月选拔赛个人题解

    这场比赛演的逼真,感谢队友不杀之恩 总结:卡题了赶紧换,手上捏着的题尽快上机解决 http://csustacm.com:4803/ 1113~1122 1113:六学家 题意:找出满足ai+aj=a ...

  5. nginx如何配置虚拟主机

    server { listen 80; #listen [::]:80 default_server ipv6only=on; server_name local.presion.caomall.ne ...

  6. poi对word的操作(总结)

    ★★★ POI在读写word docx文件时是通过xwpf模块来进行的,其核心是XWPFDocument.    1.正文段落:一个文档包含多个段落Paragraph,一个段落包含多个Runs,一个R ...

  7. ASP.NET读取RSS

    从网上找的一段读取RSS的代码,经测能用: /// <summary> /// 加载RSS /// </summary> /// <param name="Rs ...

  8. mybatis错误总结

    1:传递多个参数失败   Parameter 'username' not found. Available parameters are [0, 1, param1, param2] dao层错误写 ...

  9. 【leetcode 简单】第四十一题 Excel表列序号

    给定一个Excel表格中的列名称,返回其相应的列序号. 例如, A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...

  10. Coursera在线学习---第三节.归一化处理(Normalize)

    一.归一化(也说标准化)作用 1)将有量纲特征转化为无量纲特征 2)能够加快收敛(主要指梯度下降法时) 二.Octave中计算          mean(A)   求解矩阵中每一列的均值 std(A ...