基于Portal的WebAPI开发
基于Portal的WebAPI开发
1. 创建类库项目
2. 创建WebAPI项目
3. 使用Postman调试WebAPI
StudentInfoCommand部分:
using RekTec.Demo.Sales.Model; using RekTec.Xdk.Data; using RekTec.XStudio.Business.Core; using RekTec.XStudio.Identity; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RekTec.Demo.Sales.StudnetInfo { public class StudentInfoCommand : PortalCommand { /// <summary> /// 获取学生信息列表 /// </summary> /// <param name="name">学生名称</param> /// <param name="mobile">学生电话</param> /// <param name="orderby"></param> /// <param name="pageSize"></param> /// <param name="pageIndex"></param> /// <returns></returns> public StudentInfoData GetStudentInfoList(string name, string mobile, string orderby, int pageSize, int pageIndex) { try { StudentInfoData modelList = new StudentInfoData(); StringBuilder sb = new StringBuilder(); List<StudentInfoModel> studentInfoList = new List<StudentInfoModel>(); sb.Append("select * from studentinfo where 1=1"); Dictionary<string, object> paramList = new Dictionary<string, object>(); if (!string.IsNullOrWhiteSpace(name)) { sb.Append(" and name like '%' + @name +'%' "); paramList.Add("@name", name); } if (!string.IsNullOrWhiteSpace(mobile)) { sb.Append(" and mobile like '%' + @mobile + '%' "); paramList.Add("@mobile", mobile); } if (!string.IsNullOrWhiteSpace(orderby)) { string[] order = orderby.Split(','); sb.Append($" order by {order[0]} {order[1].Replace("descending", "desc").Replace("ascending", "asc")} ");//排序 } sb.Append($@" offset (({pageIndex}-1)*{pageSize}) rows fetch next {pageSize} rows only; "); DataTable dtStudentInfo = Broker.Query(sb.ToString(), paramList); if (dtStudentInfo == null || dtStudentInfo.Rows.Count <= 0) { modelList.studentInfoList = studentInfoList; modelList.totalRecordCount = 0; return modelList; } foreach (DataRow row in dtStudentInfo.Rows) { StudentInfoModel studentInfo = new StudentInfoModel(); if (!string.IsNullOrWhiteSpace(row["studentid"].ToString())) studentInfo.studentid = new Guid(row["studentid"].ToString()); if (!string.IsNullOrWhiteSpace(row["studentno"].ToString())) studentInfo.studentno = row["studentno"].ToString(); studentInfo.name = row["name"].ToString(); studentInfo.mobile = row["mobile"].ToString(); if (!string.IsNullOrWhiteSpace(row["gender"].ToString())) studentInfo.gender = row["gender"].ToString(); /*if (!string.IsNullOrWhiteSpace(row["age"].ToString())) studentInfo.age = Convert.ToInt32(row["age"].ToString());*/ studentInfo.classname = row["classname"].ToString(); if (!string.IsNullOrWhiteSpace(row["createdon"].ToString())) studentInfo.createdon = Convert.ToDateTime(row["createdon"]).ToString("yyyy-MM-dd"); studentInfoList.Add(studentInfo); } modelList.studentInfoList = studentInfoList; modelList.totalRecordCount = GetStudentInfoListTotalCount(name, mobile); return modelList; } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// 获取学生列表的条数 /// </summary> /// <param name="name">客户名称</param> /// <param name="mobile">手机号</param> /// <returns></returns> public int GetStudentInfoListTotalCount(string name, string mobile) { StringBuilder sb = new StringBuilder(); sb.Append(@"select count(1) num from studentinfo where 1=1"); Dictionary<string, object> paramList = new Dictionary<string, object>(); if (!string.IsNullOrWhiteSpace(name)) { sb.Append(@" and name like '%' + @name +'%' "); paramList.Add("@name", name); } if (!string.IsNullOrWhiteSpace(mobile)) { sb.Append(" and mobile like '%' + @mobile + '%' "); paramList.Add("@mobile", mobile); } DataTable dtStudentInfo = Broker.Query(sb.ToString(), paramList); if (dtStudentInfo != null && dtStudentInfo.Rows.Count > 0) { return Convert.ToInt32(dtStudentInfo.Rows[0]["num"].ToString()); } return 0; } /// <summary> /// 获取单个学生信息 /// </summary> /// <param name="studentid"></param> /// <returns></returns> public StudentInfoModel GetStudentIdById(string studentinfoId) { if (string.IsNullOrWhiteSpace(studentinfoId)) throw new Exception("学生Id丢失!"); string sqlStudentInfo = @"select * from studentinfo where studentid = @studentinfoId"; Dictionary<string, object> paramList = new Dictionary<string, object>(); paramList.Add("@studentinfoId", studentinfoId); DataTable dtStudentInfo = Broker.Query(sqlStudentInfo, paramList); if (dtStudentInfo?.Rows?.Count <= 0) return new StudentInfoModel(); DataRow row = dtStudentInfo.Rows[0]; StudentInfoModel studentInfo = new StudentInfoModel(); if (!string.IsNullOrWhiteSpace(row["studentid"].ToString())) studentInfo.studentid = new Guid(row["studentid"].ToString()); studentInfo.studentno = row["studentno"].ToString(); studentInfo.name = row["name"].ToString(); studentInfo.mobile = row["mobile"].ToString(); studentInfo.gender = row["gender"].ToString(); studentInfo.classname = row["classname"].ToString(); studentInfo.age = row["age"].ToString(); if (!string.IsNullOrWhiteSpace(row["createdon"].ToString())) studentInfo.createdon = Convert.ToDateTime(row["createdon"]).ToString("yyyy-MM-dd"); return studentInfo; } /// <summary> /// 创建或更新学生信息 /// </summary> /// <param name="studentInfoModel"></param> /// <returns></returns> public string CreateOrUpdateStudentInfo(StudentInfoModel studentInfoModel) { Dictionary<string, object> paramList = new Dictionary<string, object>(); paramList.Add("@studentno", studentInfoModel.studentno); paramList.Add("@name", studentInfoModel.name); paramList.Add("@mobile", studentInfoModel.mobile); paramList.Add("@gender", studentInfoModel.gender); paramList.Add("@classname", studentInfoModel.classname); paramList.Add("@age", studentInfoModel.age); Guid studentinfoId = studentInfoModel.studentid; bool isCreate = true; if (studentinfoId != Guid.Empty) { string sqlStudentInfo = @"select studentid from studentinfo where studentid = @studentid"; paramList.Add("@studentid", studentinfoId); DataTable dtStudentInfo = Broker.Query(sqlStudentInfo, paramList); if (dtStudentInfo.Rows.Count > 0) isCreate = false; } else { studentinfoId = Guid.NewGuid(); paramList.Add("@studentid", studentinfoId); } string sqlStudentInfoProcess = string.Empty; if (isCreate) { sqlStudentInfoProcess = @"INSERT INTO studentinfo(studentid,studentno,name,mobile,gender,classname,age,createdon) VALUES(@studentid,@studentno,@name,@mobile,@gender,@classname,@age,GETDATE())"; } else { sqlStudentInfoProcess = @"UPDATE stuedentinfo SET studentno = @studentno,name = @name,mobile=@mobile, gender=@gender, classname = @classname,age = @age,createdon = GETDATE() WHERE studentid = @studentid"; } Broker.Execute(sqlStudentInfoProcess, paramList); return studentinfoId.ToString(); } /// <summary> /// 删除学生列表 /// </summary> /// <param name="studentIds"></param> /// <returns></returns> public string DeleteStudentInfo(string studentIds) { try { if (string.IsNullOrWhiteSpace(studentIds)) throw new Exception("学生id列表为空!"); string sqlStudentInfo = $@"delete from studentinfo where studentid in ('{studentIds.Replace(",", "','")}')"; return Broker.Execute(sqlStudentInfo).ToString(); } catch (Exception ex) { throw new Exception(ex.Message); } } } }
StudentInfoModel部分代码:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RekTec.Demo.Sales.StudnetInfo { public class StudentInfoModel { /// <summary> /// 学生id /// </summary> [Key] public Guid studentid { get; set; } /// <summary> /// 学生编号 /// </summary> public string studentno { get; set; } /// <summary> /// 学生姓名 /// </summary> public string name { get; set; } /// <summary> /// 手机电话 /// </summary> public string mobile { get; set; } /// <summary> /// 性别 /// </summary> public string gender { get; set; } /// <summary> /// 班级 /// </summary> public string classname { get; set; } /// <summary> /// 年龄 /// </summary> public string age { get; set; } /// <summary> /// 创建时间 /// </summary> public string createdon { get; set; } } public class StudentInfoData { /// <summary> /// 学生列表 /// </summary> public List<StudentInfoModel> studentInfoList { get; set; } /// <summary> /// 总记录数 /// </summary> public int totalRecordCount { get; set; } } }
StudentInfoController部分代码:
using RekTec.Demo.Sales.StudnetInfo; using RekTec.XStudio.WebApi.Core; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; namespace RekTec.Demo.Sales.WebApi.Controller { [AllowAnonymous] [RoutePrefix("api/StudentInfo")] public class StudentInfoController : BaseApiController { /// <summary> /// 获取学生信息 /// </summary> /// <param name="name">学生名称</param> /// <param name="mobile">手机号</param> /// <returns></returns> [HttpGet, Route("GetStudentInfoList")] public StudentInfoData GetStudentInfoList(string name, string mobile, string orderby, int pageSize, int pageIndex) { return new StudentInfoCommand().GetStudentInfoList(name, mobile, orderby, pageSize, pageIndex); } /// <summary> /// 获取单条学生信息 /// </summary> /// <param name="studentid">学生id</param> /// <returns></returns> [HttpGet, Route("GetStudentInfoById")] public StudentInfoModel GetStudenIdById(string studentinfoId) { return new StudentInfoCommand().GetStudentIdById(studentinfoId); } /// <summary> /// 创建学生信息 /// </summary> /// <param name="studentInfoModel"></param> /// <returns></returns> [HttpPost, Route("CreateStudentInfo")] public string CreateStudentInfo(StudentInfoModel studentInfoModel) { return new StudentInfoCommand().CreateOrUpdateStudentInfo(studentInfoModel); } /// <summary> /// 更新学生信息 /// </summary> /// <param name="studentInfoModel"></param> /// <returns></returns> [HttpPost, Route("UpdateStudentInfo")] public string UpdateStudentInfo(StudentInfoModel studentInfoModel) { return new StudentInfoCommand().CreateOrUpdateStudentInfo(studentInfoModel); } /// <summary> /// 批量删除学生信息 /// </summary> /// <param name="studentIds"></param> /// <returns></returns> [HttpPost, Route("DeleteStudentInfo")] public string DeleteStudentInfo(string studentIds) { return new StudentInfoCommand().DeleteStudentInfo(studentIds); } } }
数据库连接部分:
Password=123456;User ID=sa;Initial Catalog=student;Data Source=.
根据自己的环境配置进行修改。
注:本项目使用瑞泰的NuGet包,请大家自己进行搜索配置。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现