# web_api_demo101 **Repository Path**: his0769/web_api_demo101 ## Basic Information - **Project Name**: web_api_demo101 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-23 - **Last Updated**: 2025-10-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: dotNET ## README # 01 视频地址 ``` https://www.bilibili.com/video/BV19C411H7z9/?spm_id_from=333.1391.0.0&vd_source=b223749b373286492506afea4192215d ``` # 01 项目结构 - Transient, Scoped, Singleton, ``` 01接口 Demo.Api |- Controllers |- UserController 02应用服务 Demo.Application |- Service |- Interface/IUserService |- UserService 03领域 Demo.Domain |- DbContexts |- DbContext |- Entities |- User |- Repositories |- UserRepository |- IUserRepository |- Demo.Data.EF |- UserEFRepository |- Demo.Data.NH |- UserNHRepository Controllers, Services, Services.Impl, Repositories, Repositories.Impl, Entities, Dtos DbContext AddTransient, AddScoped, AddSingleton ``` # 02 MVC 基础路由 ``` [ApiController] [Route("api/[controller]")] // 基础路由:api/users public class UsersController : ControllerBase { [HttpGet] // 完整路由:api/users public IActionResult GetAll() { ... } [HttpGet("{id}")] // 完整路由:api/users/123 public IActionResult GetById(int id) { ... } [HttpPost("create")] // 完整路由:api/users/create public IActionResult Create(User user) { ... } } ``` # 03 SqlSugar ``` SqlSugarCore : 5.1.4.205 [Sql]:SELECT COUNT(1) FROM `Student` [Sql]:SELECT `Id`,`SchoolId`,`Name`,`Created`,`Updated` FROM `Student` LIMIT 0,10 ``` ## 表名列名启用下划线 ``` SqlSugar中,CodeFirst.InitTables, 建表时, 可以设置规则吗? 比如 : 驼峰方式, 下划线间隔, 等等 https://www.doubao.com/thread/w6632851f50e74475 2.3 建表技巧:启用下划线 , 通过实体AOP实现 https://www.donet5.com/Home/Doc?typeId=1206 ConfigureExternalServices = new ConfigureExternalServices() { //处理列名 EntityService = (x, p) => { p.DbColumnName = UtilMethods.ToUnderLine(p.DbColumnName); }, //处理表名 EntityNameService = (x, p) => { p.DbTableName = UtilMethods.ToUnderLine(p.DbTableName); p.IsDisabledDelete = true; //禁止删除 } } # 禁止删除“列” p.IsDisabledDelete = true; //禁止删除 ``` ## sqlite ``` Unhandled exception. SqlSugar.SqlSugarException: 中文提示 : Sqlite不支持修改主键 更新表名和字段名,采用下划线方式, 但是报错, sqlite 不支持修改主键 ``` # 04 交给 DI 容器管理 - 除了这种“面向接口”编程的,可以添加到 DI 容器中 : `builder.Services.AddScoped();` - 大对象也可以先 new 出来,再添加到 DI 容器中: ``` SqlSugarClient dbClient = SqlSugarDbClient.GetSqlSugarDbClient(); //builder.Services.AddSingleton(dbClient); // 数据库连接 builder.Services.AddSingleton(dbClient); // 数据库连接 ``` # 05 接收和解析POST数据 ``` ASP.NET Core 如何接收和解析 POST 传递的数据 https://www.doubao.com/thread/w45de7a95efa5adc6 [FromForm] [FromBody] ```