









.NET开源商城CoreShop简记
<h1>牛腩测试一下</h1> <input id="aaa" class="layui-input" /> <button type="button" class="layui-btn" onclick="aaa()">添加数据测试</button> <script> function aaa() { layui.use([ 'laydate'], function () { var $ = layui.$, laydate = layui.laydate; laydate.render({ elem: '#aaa' }); var url = layui.setter.apiUrl + "Api/NiunanUserInfo/Test"; var postdata = { username: "niunan", password: "123456" } $.get(url, postdata, function (data) { alert("服务端返回:" + JSON.stringify(data)); }); }); } </script>
6. 接下来做自己的后端接口,流程:数据库中建表 --》2.Entity项目中建立模型 --》4.Repository里建立仓储接口和仓储实现 --》3.Services中建立服务接口和实现 --》9.App 中的CoreCms.Net.Web.Admin项目中建立控制器来实现接口
create table NiunanUserInfo( Id int primary key identity, CreateTime datetime not null default getdate(), UpdateTime datetime not null default getdate(), UserName nvarchar(50) not null default '' )
// CoreCms.Net.Model\Entities\NiunanDemo\NiunanUserInfo.cs using SqlSugar; using System.ComponentModel.DataAnnotations; namespace CoreCms.Net.Model.Entities { /// <summary> /// 牛腩自己建的用户表 /// </summary> [SugarTable("NiunanUserInfo", TableDescription = "牛腩用户表")] public partial class NiunanUserInfo { /// <summary> /// 用户表 /// </summary> public NiunanUserInfo() { } /// <summary> /// 用户ID /// </summary> [Display(Name = "用户ID")] [SugarColumn(ColumnDescription = "用户ID", IsPrimaryKey = true, IsIdentity = true)] [Required(ErrorMessage = "请输入{0}")] public System.Int32 Id { get; set; } /// <summary> /// UserName /// </summary> [Display(Name = "UserName")] [SugarColumn(ColumnDescription = "UserName", IsNullable = true)] [StringLength(50, ErrorMessage = "【{0}】不能超过{1}字符长度")] public System.String UserName { get; set; } /// <summary> /// 创建时间 /// </summary> [Display(Name = "创建时间")] [SugarColumn(ColumnDescription = "创建时间", IsNullable = true)] public System.DateTime CreateTime { get; set; } = System.DateTime.Now; /// <summary> /// 更新时间 /// </summary> [Display(Name = "更新时间")] [SugarColumn(ColumnDescription = "更新时间", IsNullable = true)] public System.DateTime UpdateTime { get; set; } = System.DateTime.Now; } }
// CoreCms.Net.IRepository\NiunanDemo\INiunanUserInfoRepository.cs using CoreCms.Net.Model.Entities; namespace CoreCms.Net.IRepository { public interface INiunanUserInfoRepository : IBaseRepository<NiunanUserInfo> { } } // CoreCms.Net.Repository\NiunanDemo\NiunanUserInfoRepository.cs using CoreCms.Net.IRepository; using CoreCms.Net.IRepository.UnitOfWork; using CoreCms.Net.Model.Entities; namespace CoreCms.Net.Repository { public class NiunanUserInfoRepository : BaseRepository<NiunanUserInfo>, INiunanUserInfoRepository { public NiunanUserInfoRepository(IUnitOfWork unitOfWork) : base(unitOfWork) { } } }
// CoreCms.Net.IServices\NiunanDemo\INiunanUserInfoServices.cs using System.Collections.Generic; using System.Threading.Tasks; using CoreCms.Net.Model.Entities; namespace CoreCms.Net.IServices { public interface INiunanUserInfoServices : IBaseServices<NiunanUserInfo> { } } // CoreCms.Net.Services\NiunanDemo\NiunanUserInfoServices.cs using CoreCms.Net.IRepository; using CoreCms.Net.IRepository.UnitOfWork; using CoreCms.Net.IServices; using CoreCms.Net.Model.Entities; namespace CoreCms.Net.Services { public class NiunanUserInfoServices : BaseServices<NiunanUserInfo>, INiunanUserInfoServices { private readonly INiunanUserInfoRepository _dal; private readonly IUnitOfWork _unitOfWork; public NiunanUserInfoServices(IUnitOfWork unitOfWork, INiunanUserInfoRepository dal) { _dal = dal; BaseDal = dal; _unitOfWork = unitOfWork; } } }
// CoreCms.Net.Web.Admin\Controllers\NiunanDemo\NiunanUserInfoController.cs using CoreCms.Net.Configuration; using CoreCms.Net.IServices; using CoreCms.Net.Model.Entities; using CoreCms.Net.Web.Admin.Infrastructure; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.ComponentModel; namespace CoreCms.Net.Web.Admin.Controllers { /// <summary> /// 牛腩测试 /// </summary> [Description("牛腩测试")] [Route("api/[controller]/[action]")] [ApiController] [RequiredErrorForAdmin] [Authorize(Permissions.Name)] public class NiunanUserInfoController : ControllerBase { private readonly INiunanUserInfoServices _services; public NiunanUserInfoController(INiunanUserInfoServices _services) { this._services = _services; } [AllowAnonymous] [HttpGet] public string Test(string username, string password) { try { int id = _services.Insert(new NiunanUserInfo() { UserName = username, }); return "插入成功,返回的ID是:"+id; } catch (Exception ex) { return "出错:" + ex.Message; } } } }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。