如果你要问我webapi是干嘛,我只能说它是的给数据。哈哈哈哈哈,这几天也才刚刚了解了解关于webapi的知识,今天就来谈谈吧。

1.创建webapi项目

第一步:选择asp.net web应用程序

第二步:选择webapi,记得要取消勾选https配置,点击创建

 第三步:创建完成后形成的项目结构

这里和asp.net mvc的目录结构看起来有点类似,其实还是有很大的不同的

在这里view视图其实不起什么作用。

下面来看看目录结构中有什么东西。

 可以看到自动创建了两个控制器分别是home和values(这里的student和user是我自己创建的)

进去看看。需要注意的是这里home和下面介绍的values是不同的控制器,哪里不同呢?等一下就知道啦

home控制器:

values控制器:

与home控制器最大的区别就在于继承的控制器前者是controller、后者是apicontroller

可以看到这里分别有几个方法:get(查询)、post(新增)、put(修改)、以及delete(删除)

[frombody]在这里的作用就是只接受从data中传递过来的参数,也就是其他传参方法比如querystring形式的方法无法获取。

为什么系统自动帮我们创建了这四个方法呢?

 1 using system;
 2 using system.collections.generic;
 3 using system.linq;
 4 using system.net;
 5 using system.net.http;
 6 using system.web.http;
 7 
 8 namespace demo.controllers
 9 {
10     //webapi控制器
11     public class valuescontroller : apicontroller
12     {
13         // get api/values
14         public ienumerable<string> get()
15         {
16             return new string[] { "value1", "value2" };
17         }
18 
19         // get api/values/5
20         public string get(int id)
21         {
22             return "value";
23         }
24 
25         // post api/values
26         public void post([frombody]string value)
27         {
28         }
29         //frombody指定数据来源必须是data中的数据而不是从querystring过来的
30         // put api/values/5
31         public void put(int id, [frombody]string value)
32         {
33         }
34 
35         // delete api/values/5
36         public void delete(int id)
37         {
38         }
39     }
40 }

这里的原有还有追溯到创建webapi项目的时候

那就是restful http服务模板

通过restful语义定义的四个方法,可以通过ajax请求进行调用并获得数据。

 下面我们去创建一个普通的html页面和一个student api控制器以及一个student类

student类:

 1 using system;
 2 using system.collections.generic;
 3 using system.linq;
 4 using system.web;
 5 
 6 namespace demo.models
 7 {
 8     public class student
 9     {
10         public int id { get; set; }
11         public string name { get; set; }
12     }
13 }

student控制器方法:

在注释中我也备注了restful使用规则和约束

 1 using demo.models;
 2 using system;
 3 using system.collections.generic;
 4 using system.linq;
 5 using system.net;
 6 using system.net.http;
 7 using system.web.http;
 8 
 9 namespace demo.controllers
10 {
11     //webapi控制器
12     public class studentcontroller : apicontroller
13     {
14         /// <summary>
15         /// restful格式的数据提供方式 方法名要带着get
16         /// restful风格节省了方法名的编写,但是不适合~login登录、上传 、修改头像、点赞等业务功能的实现
17         /// restful只适合对某一个表的增删改查
18         /// </summary>
19         /// <returns></returns>
20         public student get()
21         {
22             return new student() { id = 1, name = "黄朝" };
23         }
24         /// <summary>
25         ///get不能省略
26         /// </summary>
27         /// <param name="name"></param>
28         /// <returns></returns>
29         public string get(string name)
30         {
31             return "参数是:" + name;
32         }
33 
34         public string getaddress(string addname) {
35             return "地址参数是:" + addname;
36         }
37         /// <summary>
38         /// 在这里post是做新增操作
39         /// </summary>
40         /// <param name="stu">学生对象</param>
41         public void post(student stu)
42         { 
43             
44         }
45         /// <summary>
46         /// 修改操作
47         /// </summary>
48         /// <param name="id">修改的id</param>
49         /// <param name="stu">修改的对象</param>
50         public void put(int id,student stu)
51         { 
52             
53         }
54         /// <summary>
55         /// 删除
56         /// </summary>
57         /// <param name="id">根据id的删除</param>
58         public void delete(int id)
59         { 
60             
61 
62         }
63     }
64 }

html页:

根据type不同的参数,调用后台api的数据。可以作为对象参数传递也可以根据id作为参数传递

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="scripts/jquery-3.3.1.js"></script>
    <script>
        $.ajax({
            url: "/api/student",
            type: "get"
        }).done(function (data) { console.log(data) })

        //$.ajax({
        //    url: "/api/student",
        //    type: "post",
        //    data: {
        //        id: 1,
        //        name:"abc"
        //    }
        //}).done(function () {
        //    console.info("成功");
        //}).fail(function () {
        //    console.info("失败");
        //})

        ///api/student/1 1为id
        //$.ajax({
        //    url: "/api/student/1",
        //    type: "put",
        //    data: {
        //        id: 12,
        //        name:"lisi"
        //    }
        //}).done(function () {
        //    console.info("成功")
        //}).fail(function () {
        //    console.info("失败")
        //})

        //$.ajax({
        //    url:"/api/student/1"
        //    type: "delete"
        //}).done(function () {rl: "/api/student/1",
        //    console.info("成功")
        //}).fail(function () {
        //    console.info("失败")
        //})
</script>
</head>
<body>
        
</body>
</html>

以上就是我对初次了解webapi的一个学习总结,如果以上内容存在不足或错误代码,请指出,谢谢。