概述
全局:
两个工程中定义用于数据交换的类
成功:
public class Success
{
/// <summary>
/// 成功返回对象<br/>
/// exp:<br/>
/// <div style="color:red">登录成功</div>
/// </summary>
/// <param name="message">提示信息</param>
/// <param name="result">数据</param>
public Success(string message, object result)
{
this.code = 200;
this.message = message;
this.result = result;
}
public int code { get; set; }
public string message { get; set; }
public object result { get; set; }
}
失败:
public class Error
{
/// <summary>
/// 错误返回对象<br/>
/// exp:<br/>
/// <font style='color:red'>登录错误</font><br/>
/// 数据库连接失败<br/>
/// </summary>
/// <param name="message">提示信息</param>
public Error(string message)
{
this.code = -1;
this.message = message;
this.result = null;
}
public int code { get; set; }
public string message { get; set; }
public object result { get; set; }
}
Api方面:
返回值为成功或者失败的类 值在类中
/// <summary>
/// 查询所有
/// </summary>
/// <returns></returns>
[HttpGet]
public object QueryAllSysuser()
{
using (fashionshoppingDBEntities db = new fashionshoppingDBEntities())
{
//toList代表关闭查询(关闭长连接)
return new Success("查询成功", db.Sysuser.ToList());
}
}
/// <summary>
/// 根据名称查询用户
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
[HttpPost]
public object QuerySysuser(string username)
{
using (fashionshoppingDBEntities db = new fashionshoppingDBEntities())
{
List<Sysuser> u = null;
if (!string.IsNullOrEmpty(username))
{
u = db.Sysuser.Where(o => o.username.Contains(username)).ToList();
}
if (u == null)
{
return new Error("没有此用户");
}
return new Success("查询成功", u);
}
}
/// <summary>
/// 添加用户
/// </summary>
/// <param name="username">名称</param>
/// <param name="pwd">密码</param>
/// <param name="role">管理员状态</param>
/// <returns></returns>
[HttpPost]
public object SaveSysuser(string username,string pwd,string role)
{
if (
string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd) ||
string.IsNullOrEmpty(role)
)
{
return new Error("参数不允许为空");
}
Sysuser u = new Sysuser();
u.username = username;
u.pwd = pwd;
u.role = int.Parse(role);
using (fashionshoppingDBEntities db = new fashionshoppingDBEntities())
{
db.Sysuser.Add(u);
int rows = db.SaveChanges();
if (rows > 0)
{
return new Success("添加成功", u.id);
}
return new Error("添加失败");
}
}
/// <summary>
/// 修改用户
/// </summary>
/// <param name="id">修改的用户id</param>
/// <param name="username">修改后的name</param>
/// <param name="pwd">修改后的密码</param>
/// <param name="role">修改后的状态</param>
/// <returns></returns>
[HttpPost]
public object UpdateSysuser(int id, string username, string pwd, string role)
{
using (fashionshoppingDBEntities db = new fashionshoppingDBEntities())
{
Sysuser user = db.Sysuser.Where(o => o.id.Equals(id)).SingleOrDefault();
user.username = username;
user.pwd = pwd;
user.role = int.Parse(role);
int rows = db.SaveChanges();
if (rows > 0)
{
return new Success("更新成功", id);
}
return new Error("更新失败");
}
}
/// <summary>
/// 删除用户
/// </summary>
/// <param name="id"> 删除用户id</param>
/// <returns></returns>
[HttpGet]
public object DeleteById(string id)
{
if (string.IsNullOrEmpty(id))
{
return new Error("参数不允许为空");
}
using (fashionshoppingDBEntities db = new fashionshoppingDBEntities())
{
int Id = int.Parse(id);
Sysuser u = db.Sysuser.Where(o => o.id.Equals(Id)).SingleOrDefault();
if (u == null)
{
return new Error("没有此用户");
}
//删除操作
db.Sysuser.Remove(u);
//写入数据库
int rows = db.SaveChanges();
if (rows > 0)
{
return new Success("删除成功", rows);
}
return new Error("删除失败");
}
}
前端方面:
控制器:
封装的方法:
/// <summary>
/// get请求方法
/// </summary>
/// <param name="url">请求地址</param>
/// <returns></returns>
public Success RequestGet(string url)
{
//声明http访问
HttpClient http = new HttpClient();
//进行异步请求
Task<string> task = http.GetStringAsync(url);
//获取返回数据
string result = task.Result;
return JsonConvert.DeserializeObject<Success>(task.Result);
}
/// <summary>
/// post请求方式
/// </summary>
/// <param name="url">请求地址?数据</param>
/// <returns></returns>
public Success RequestPost(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);
}
Action方法:
//页面展示全部
public ActionResult Index()
{
//访问路径
string url = "http://localhost:16018/api/Sysuser/QueryAllSysuser";
Success success = RequestGet(url);
//请求成功
if (success.code == 200)
{
ViewBag.message = success.message;
ViewBag.lists = success.result;
}
return View();
}
页面代码:
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th>姓名</th>
<th>密码</th>
<th>角色</th>
<th>操作</th>
</tr>
@foreach (var item in ViewBag.lists)
{
<tr>
<td>@item.username</td>
<td>@item.pwd</td>
<td>
@if (item.role == 1)
{
@:系统管理员
}
else
{
@:普通管理员
}
</td>
<td>
<a href="/Sysuser/DeleteById?id=@item.id">删除</a>
<a href="/Sysuser/Update?id=@item.id">更新</a>
</td>
</tr>
}
</table>
请求链接:
查询所有:http://localhost:16018/api/Sysuser/QueryAllSysuser
按name查询:http://localhost:16018/api/Sysuser/QuerySysuser?username={0}
按id查询:"http://localhost:16018/api/Sysuser/QuertById?id=" + id
新增用户:"http://localhost:16018/api/Sysuser/SaveSysuser?username={0}&pwd={1}&role={2}", username, pwd, role
修改用户:"http://localhost:16018/api/Sysuser/UpdateSysuser?id={0}&username={1}&pwd={2}&role={3}", id, username, pwd, role
删除用户:http://localhost:16018/api/Sysuser/DeleteById?id=" + id
最后
以上就是现实电灯胆为你收集整理的asp.net前端调用后端api接口全局:Api方面:前端方面:的全部内容,希望文章能够帮你解决asp.net前端调用后端api接口全局:Api方面:前端方面:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复