• .NET MVC第十章 vue axios解析web api接口


    .NET MVC第十章 vue axios解析web api接口


    目录

    .NET MVC第十章 vue axios解析web api接口

    数据库

    EF引入数据

    创建API控制器

    接口编码

    获取接口

    网络js环境

    总结


    数据库

    我用的是之前的数据库,用作测试,数据库的名称是【girl1804】

    1. /*
    2. Navicat SQL Server Data Transfer
    3. Source Server : mysqlserver
    4. Source Server Version : 120000
    5. Source Host : .:1433
    6. Source Database : girl1804
    7. Source Schema : dbo
    8. Target Server Type : SQL Server
    9. Target Server Version : 120000
    10. File Encoding : 65001
    11. Date: 2022-10-05 20:24:26
    12. */
    13. -- ----------------------------
    14. -- Table structure for [dbo].[girlSix]
    15. -- ----------------------------
    16. DROP TABLE [dbo].[girlSix]
    17. GO
    18. CREATE TABLE [dbo].[girlSix] (
    19. [id] varchar(32) NOT NULL DEFAULT (replace(newid(),'-','')) ,
    20. [createDate] datetime NOT NULL DEFAULT (getdate()) ,
    21. [nickName] varchar(30) NOT NULL ,
    22. [introduce] nvarchar(200) NOT NULL
    23. )
    24. GO
    25. IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description',
    26. 'SCHEMA', N'dbo',
    27. 'TABLE', N'girlSix',
    28. 'COLUMN', N'nickName')) > 0)
    29. EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'名字'
    30. , @level0type = 'SCHEMA', @level0name = N'dbo'
    31. , @level1type = 'TABLE', @level1name = N'girlSix'
    32. , @level2type = 'COLUMN', @level2name = N'nickName'
    33. ELSE
    34. EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'名字'
    35. , @level0type = 'SCHEMA', @level0name = N'dbo'
    36. , @level1type = 'TABLE', @level1name = N'girlSix'
    37. , @level2type = 'COLUMN', @level2name = N'nickName'
    38. GO
    39. IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description',
    40. 'SCHEMA', N'dbo',
    41. 'TABLE', N'girlSix',
    42. 'COLUMN', N'introduce')) > 0)
    43. EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'介绍'
    44. , @level0type = 'SCHEMA', @level0name = N'dbo'
    45. , @level1type = 'TABLE', @level1name = N'girlSix'
    46. , @level2type = 'COLUMN', @level2name = N'introduce'
    47. ELSE
    48. EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'介绍'
    49. , @level0type = 'SCHEMA', @level0name = N'dbo'
    50. , @level1type = 'TABLE', @level1name = N'girlSix'
    51. , @level2type = 'COLUMN', @level2name = N'introduce'
    52. GO
    53. -- ----------------------------
    54. -- Records of girlSix
    55. -- ----------------------------
    56. INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'04e3d962adcb4a5b8fefaf8b46995e85', N'2020-05-27 09:05:52.000', N'董新颖', N'郭老师关门弟子之一。');
    57. GO
    58. INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'568fc305930347d3bec1ddd08c71ad29', N'2020-05-27 09:01:09.000', N'王笑涵', N'北方有佳人,绝世而独立。');
    59. GO
    60. INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'7eaa67475f0c4086a5e76ba1731196d2', N'2022-06-12 11:23:16.000', N'夏天', N'保持每天好心情。');
    61. GO
    62. INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'972ec358089042e0bf24fd9efca47bde', N'2020-05-27 08:59:49.000', N'牛龙珠', N'笑若桃花三月开,清风徐徐醉颜来。');
    63. GO
    64. INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'BDFFC6A36A53408281EB8CA242C0E7A3', N'2020-05-27 08:42:31.000', N'闫春娜', N'珠缨旋转星宿摇,花蔓抖擞龙蛇动。');
    65. GO
    66. INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'd1cdd67717e549caba16503787b55877', N'2021-02-17 15:27:41.357', N'小龙女', N'想过过过儿过过的日子');
    67. GO
    68. INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'efb0ca854dac456b9d8c42d4c4b1bce0', N'2020-05-27 09:03:30.000', N'刘梓佳', N'明眸善睐,辅靥承权,瑰姿艳逸,怡静体闲,端的是好一个花王,富贵的牡丹。');
    69. GO
    70. INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'f839343b980e45caafaa9d2c9797294b', N'2020-05-27 09:04:53.000', N'魏慧娟', N'脉脉眼中波,盈盈花盛处。');
    71. GO
    72. -- ----------------------------
    73. -- Indexes structure for table girlSix
    74. -- ----------------------------
    75. -- ----------------------------
    76. -- Primary Key structure for table [dbo].[girlSix]
    77. -- ----------------------------
    78. ALTER TABLE [dbo].[girlSix] ADD PRIMARY KEY ([id])
    79. GO

    EF引入数据

    等待完成后即可。

    我们需要使用的是girl1804Entities

    创建API控制器

    接口编码

    为了方便操作,我都写成HTTPGET请求了

    获取信息

    1. ///
    2. /// 获取信息接口
    3. ///
    4. ///
    5. public object GetInfo()
    6. {
    7. using (girl1804Entities db = new girl1804Entities())
    8. {
    9. return db.girlSix.ToList();
    10. }
    11. }

    添加信息

    1. ///
    2. /// 添加
    3. ///
    4. ///
    5. ///
    6. ///
    7. [HttpGet]
    8. public bool AddInfo(string nickName, string introduce)
    9. {
    10. using (girl1804Entities db = new girl1804Entities())
    11. {
    12. girlSix g = new girlSix();
    13. g.id = System.Guid.NewGuid().ToString("N");
    14. g.createDate = DateTime.Now;
    15. g.nickName = nickName;
    16. g.introduce = introduce;
    17. db.girlSix.Add(g);
    18. return db.SaveChanges() > 0;
    19. }
    20. }

    删除信息

    1. ///
    2. /// 删除
    3. ///
    4. ///
    5. ///
    6. [HttpGet]
    7. public object DeleteById(string id)
    8. {
    9. using (girl1804Entities db = new girl1804Entities())
    10. {
    11. girlSix g = db.girlSix.Where(o => o.id == id).SingleOrDefault();
    12. db.girlSix.Remove(g);
    13. return db.SaveChanges() > 0;
    14. }
    15. }

    单个查询

    1. ///
    2. /// 单个查询
    3. ///
    4. ///
    5. ///
    6. [HttpGet]
    7. public object SelectById(string id)
    8. {
    9. using (girl1804Entities db = new girl1804Entities())
    10. {
    11. return db.girlSix.Where(o => o.id == id).SingleOrDefault();
    12. }
    13. }

    修改方法

    1. ///
    2. /// 修改方法
    3. ///
    4. ///
    5. ///
    6. ///
    7. ///
    8. [HttpGet]
    9. public bool UpdateById(string id, string nickName, string introduce)
    10. {
    11. using (girl1804Entities db = new girl1804Entities())
    12. {
    13. girlSix girlSix = db.girlSix.Where(o => o.id == id).SingleOrDefault();
    14. if (girlSix == null)
    15. {
    16. return false;
    17. }
    18. girlSix.nickName = nickName;
    19. girlSix.introduce = introduce;
    20. return db.SaveChanges()>0;
    21. }
    22. }

    获取接口

     

    接口

    1. http://localhost:1246/api/gril1804/GetInfo
    2. http://localhost:1246/api/gril1804/AddInfo?nickName={nickName}&introduce={introduce}
    3. http://localhost:1246/api/gril1804/DeleteById/{id}
    4. http://localhost:1246/api/gril1804/SelectById/{id}
    5. http://localhost:1246/api/gril1804/UpdateById/{id}?nickName={nickName}&introduce={introduce}

    根据接口解析操作即可。

    网络js环境

    bootstrap-css:【

    JQuery:【

    bootstrap:【

    vue:【

    axios:【

    1. <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    2. <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js">script>
    3. <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js">script>
    4. <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js">script>
    5. <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js">script>

    完整示例:

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    9. head>
    10. <body>
    11. <div id="app">
    12. <div class="input-group">
    13. <span class="input-group-addon">昵称搜索:span>
    14. <input type="text" v-model="selectKey" placeholder="请输入搜索昵称关键字" class="form-control" />
    15. div>
    16. <table class="table table-bordered table-hover">
    17. <tr class="info">
    18. <th>编号th>
    19. <th>创建时间th>
    20. <th>昵称th>
    21. <th>介绍th>
    22. <th>操作th>
    23. tr>
    24. <tr v-for="item in newlist">
    25. <td>{{item.id}}td>
    26. <td>{{item.createDate}}td>
    27. <td>{{item.nickName}}td>
    28. <td>{{item.introduce}}td>
    29. <td>
    30. <button v-on:click="del(item.id)" class="btn btn-info">删除button>
    31. <button v-on:click="SetInfo(item)" class="btn btn-info">修改button>
    32. td>
    33. tr>
    34. table>
    35. <hr/>
    36. <div class="input-group">
    37. <span class="input-group-addon">编号:span>
    38. <input type="text" v-model="id" disabled class="form-control" />
    39. div>
    40. <div class="input-group">
    41. <span class="input-group-addon">创建时间:span>
    42. <input type="text" v-model="createDate" disabled class="form-control" />
    43. div>
    44. <div class="input-group">
    45. <span class="input-group-addon">昵称:span>
    46. <input type="text" v-model="nickName" class="form-control" />
    47. div>
    48. <div class="input-group">
    49. <span class="input-group-addon">简介:span>
    50. <input type="text" v-model="introduce" class="form-control" />
    51. div>
    52. <button v-on:click="Setting()" class="btn btn-info">完成修改button>
    53. <button v-on:click="add()" class="btn btn-info">添加button>
    54. div>
    55. <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js">script>
    56. <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js">script>
    57. <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js">script>
    58. <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js">script>
    59. <script>
    60. var urlBase = "http://localhost:1246/api/gril1804/";
    61. new Vue({
    62. el: "#app",
    63. data: {
    64. list: [],
    65. selectKey: "",
    66. id: "",
    67. createDate: "",
    68. nickName: "",
    69. introduce: ""
    70. },
    71. created() { //获取数据
    72. var _this = this;
    73. var url = urlBase + "GetInfo";
    74. axios.get(url).then(function(retult) {
    75. console.log(retult.data);
    76. _this.list = retult.data;
    77. })
    78. },
    79. computed: { //过滤数据
    80. newlist: function() {
    81. var _this = this;
    82. console.log(_this.list);
    83. return _this.list.filter(function(o) {
    84. return o.nickName.indexOf(_this.selectKey) != -1;
    85. });
    86. }
    87. },
    88. methods: { //方法集合
    89. add: function() {
    90. var url = urlBase + "addInfo?nickName=" + this.nickName + "&introduce=" + this.introduce;
    91. axios.get(url).then(function(retult) {
    92. if (retult.data) {
    93. alert("添加成功");
    94. location.reload();
    95. }
    96. })
    97. },
    98. del: function(o) {
    99. if (confirm("是否删除此行")) {
    100. var url = urlBase + "DeleteById?id=" + o;
    101. axios.get(url).then(function(retult) {
    102. alert("删除成功");
    103. location.reload();
    104. })
    105. }
    106. },
    107. SetInfo: function(item) { //修改1
    108. this.id = item.id;
    109. this.createDate = item.createDate;
    110. this.nickName = item.nickName;
    111. this.introduce = item.introduce;
    112. },
    113. Setting: function() { //修改2
    114. var url = urlBase + "UpdateById?id=" + this.id + "&nickName=" + this.nickName + "&introduce=" + this.introduce;
    115. axios.get(url).then(function(retult) {
    116. if (retult.data) {
    117. alert("修改成功");
    118. location.reload();
    119. }
    120. })
    121. }
    122. }
    123. })
    124. script>
    125. body>
    126. html>

    总结

    axios使用方法相对更方便一些,加载数据也只需要在created中处理即可,如果有需要的话就搭建一个脚手架,如果不方便话使用那个都无所谓的,功能是肯定可以实现的,前提是接口一定要跨域啊,如果接口没有跨域是无法进行解析的。

  • 相关阅读:
    自定义MVC框架
    共话云原生数据库的未来
    一文知晓Linux文件权限
    Python中文件操作(IO流)及文件备份
    二次开发真的很难吗?低代码平台有话说
    关于粉丝需要编译python版本的opencv-cuda11的方法(在ubuntu22.04平台编译,python3.6,cuda11.7)
    游戏引擎分层简介
    vscode c++食用指南
    ChatGPT AIGC 实现Excel行列多条件交叉查找
    测试与FastAPI应用数据之间的差异
  • 原文地址:https://blog.csdn.net/feng8403000/article/details/127176923