ASP.NET MVC-访问api进行可视化增删改查
2021/10/5 11:11:27
本文主要是介绍ASP.NET MVC-访问api进行可视化增删改查,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
访问api进行可视化增删改查
创建mvc项目
创建两个方法,用于进行get和post异步访问api
//get public Success GetSuccess(string url) { HttpClient http = new HttpClient(); //进行异步请求 Task<string> task = http.GetStringAsync(url); //获取返回数据 string result = task.Result; return JsonConvert.DeserializeObject<Success>(result); } //post public Success PostSuccess(string url) { HttpClient http = new HttpClient(); var str = "";//请求数据。这里为空 HttpContent content = new StringContent(str); Task<HttpResponseMessage> postTask = http.PostAsync(url, content); HttpResponseMessage result = postTask.Result;//拿到网络请求结果 result.EnsureSuccessStatusCode();//抛出异常 Task<string> task = result.Content.ReadAsStringAsync();//异步读取数据 //发送值前台 return JsonConvert.DeserializeObject<Success>(task.Result); }
查询
public ActionResult Index() { string url = "http://localhost:61840/api/Test/GetInfo"; Success success = GetSuccess(url); //请求成功 if (success.code == 200) { ViewBag.message = success.message; ViewBag.lists = success.result; } return View(); }
页面
<hr /> <a href="~/Home/AddPage">添加</a> <table class="table table-bordered table-hover"> <tr> <th>编号</th> <th>创建时间</th> <th>昵称</th> <th>简介</th> <th>操作</th> </tr> @foreach (var item in ViewBag.lists) { <tr> <td>@item.id</td> <td>@item.createDate</td> <td>@item.nickName</td> <td>@item.introduce</td> <td> <a href="~/Home/DeleteById?id=@item.id" class="btn btn-primary" onclick="return confirm('是否删除此行?')">删除</a> <a href="~/Home/UpdatePage?id=@item.id" class="btn btn-primary">修改</a> </td> </tr> } </table>
删除
public void DeleteById(string id) { string url = "http://localhost:61840/api/Test/DeleteById?id=" + id; Success success = PostSuccess(url); if (success.code==200) { Response.Write("<script>alert('" + success.message + "');window.location.href='Index'</script>"); } }
添加
public ActionResult AddPage() { return View(); }
public void AddInfo(string nickName, string introduce) { string url = $"http://localhost:61840/api/Test/AddInfo?nickName={nickName}&introduce={introduce}"; Success success = PostSuccess(url); if (success.code == 200) { Response.Write("<script>alert('" + success.message + "');window.location.href='Index'</script>"); } else { Response.Write("<script>alert('" + success.message + "');window.location.href='AddPage'</script>"); } }
页面
<h2>添加数据</h2> <form action="~/Home/AddInfo" method="post"> <p> 昵称:<input type="text" name="nickName" placeholder="请输入昵称" /> </p> <p> 简介:<input type="text" name="introduce" placeholder="请输入简介" /> </p> <p> <input type="submit" value="提交" /> </p> </form>
修改
[HttpPost] public void UpdateById(string id,string nickName,string introduce) { string url = $"http://localhost:61840/api/Test/UpdateById?id={id}&nickName={nickName}&introduce={introduce}"; Success success = PostSuccess(url); if (success.code == 200) { Response.Write("<script>alert('" + success.message + "');window.location.href='Index'</script>"); } else { Response.Write("<script>alert('" + success.message + "');window.location.href='UpdatePage'</script>"); } }
public ActionResult UpdatePage(string id) { string url = "http://localhost:61840/api/Test/SelectById?id=" + id; Success success = GetSuccess(url); //请求成功 if (success.code == 200) { ViewBag.message = success.message; ViewBag.gird = success.result; } return View(); }
页面
<h2>修改数据</h2> <form action="~/Home/UpdateById" method="post"> <p> 编号:<input type="text" name="id" readonly="readonly" value="@ViewBag.gird.id" /> </p> <p> 创建时间:<input type="text" name="createDate" readonly="readonly" value="@ViewBag.gird.createDate" /> </p> <p> 昵称:<input type="text" name="nickName" placeholder="请输入昵称" value="@ViewBag.gird.nickName" /> </p> <p> 简介:<input type="text" name="introduce" placeholder="请输入简介" value="@ViewBag.gird.introduce" /> </p> <p> <input type="submit" value="提交" /> </p> </form>
这篇关于ASP.NET MVC-访问api进行可视化增删改查的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID
- 2024-01-23用CI/CD工具Vela部署Elasticsearch + C# 如何使用
- 2024-01-23.NET开源的简单、快速、强大的前后端分离后台权限管理系统