访问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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。