$npx -y skills add youlaitech/youlai-skills --skill aspnetASP.NET Core backend development standards. Use this skill when developing ASP.NET Core projects, implementing REST APIs, using Entity Framework Core, or JWT authentication.
| 1 | # ASP.NET Core 后端开发规范 |
| 2 | |
| 3 | ## 触发条件 |
| 4 | |
| 5 | - Develop ASP.NET Core projects |
| 6 | - Implement REST APIs |
| 7 | - Use Entity Framework Core for data access |
| 8 | - Implement JWT authentication |
| 9 | - Implement permission control |
| 10 | |
| 11 | --- |
| 12 | |
| 13 | ## Part 1: 技术栈 |
| 14 | |
| 15 | | 层 | 选型 | 说明 | |
| 16 | |----|------|------| |
| 17 | | 运行环境 | **.NET 8+** | 跨平台运行时 | |
| 18 | | Web 框架 | **ASP.NET Core** | 内置依赖注入、中间件管道 | |
| 19 | | ORM | **Entity Framework Core** | LINQ 查询、Code First、迁移 | |
| 20 | | 数据库 | **MySQL 8.x** | InnoDB 引擎 | |
| 21 | | 缓存 | **Redis 7.x** | 分布式缓存 | |
| 22 | | 认证 | **JWT** (Microsoft.AspNetCore.Authentication.JwtBearer) | Token 认证 | |
| 23 | | API 文档 | **Swashbuckle** | OpenAPI 生成 | |
| 24 | |
| 25 | --- |
| 26 | |
| 27 | ## Part 2: 目录结构 |
| 28 | |
| 29 | ``` |
| 30 | src/ |
| 31 | ├── Youlai.Api/ # API 层(Controller + 中间件) |
| 32 | │ ├── Program.cs # 启动配置 |
| 33 | │ ├── Controllers/ # 按模块分目录,复数命名 |
| 34 | │ │ ├── Auth/{AuthController, WxMaAuthController} |
| 35 | │ │ ├── System/{UsersController, RolesController, MenusController, ...} |
| 36 | │ │ ├── Codegen/{CodegenController} |
| 37 | │ │ ├── File/{FilesController} |
| 38 | │ │ └── Message/{SseController} |
| 39 | │ ├── Filters/ # LogActionFilter |
| 40 | │ ├── Middlewares/ # ExceptionHandlingMiddleware, RateLimitMiddleware |
| 41 | │ ├── Security/ # CurrentUser, HasPermAttribute, JwtExtensions, PermissionAuthorizationHandler |
| 42 | │ └── Swagger/ # FileUploadOperationFilter |
| 43 | │ |
| 44 | ├── Youlai.Application/ # 应用层(Service + DTO) |
| 45 | │ ├── DependencyInjection.cs |
| 46 | │ ├── Auth/ # IAuthService, AuthService, WxMaAuthService + Models/ |
| 47 | │ ├── Common/ # ILoggingService, SseService |
| 48 | │ ├── Constants/ # RedisKeyConstants |
| 49 | │ ├── Exceptions/ # BusinessException |
| 50 | │ ├── Options/ # SecurityOptions, RedisOptions, WxMaOptions |
| 51 | │ ├── Results/ # Result, PageResult, ResultCode |
| 52 | │ ├── Security/ # ICurrentUser, JwtTokenManager, DataPermissionService + Models/ |
| 53 | │ └── System/ # IUserService, UserService 等 + Models/{User,Role,Menu,Dept,...}/ |
| 54 | │ |
| 55 | ├── Youlai.Domain/ # 领域层(最薄,无依赖) |
| 56 | │ ├── Entities/ # SysUser, SysRole, SysMenu 等 |
| 57 | │ └── Enums/ # ActionType, LogModule, MenuType |
| 58 | │ |
| 59 | ├── Youlai.Infrastructure/ # 基础设施层 |
| 60 | │ ├── DependencyInjection.cs |
| 61 | │ ├── Persistence/AppDbContext.cs |
| 62 | │ ├── FileStorage/ # LocalFileStorage, MinioFileStorage |
| 63 | │ └── CodegenTemplates/ # Scriban 模板 |
| 64 | │ |
| 65 | └── tests/Youlai.Api.Tests/ # 集成测试 |
| 66 | ``` |
| 67 | |
| 68 | **设计原则**: |
| 69 | - `Youlai.Domain` 是最薄层,不依赖任何层 |
| 70 | - `Youlai.Application` 依赖 `Domain`,不依赖 `Api` 和 `Infrastructure` |
| 71 | - `Youlai.Infrastructure` 实现接口,依赖 `Application` |
| 72 | - `Youlai.Api` 作为入口,组合所有层 |
| 73 | |
| 74 | --- |
| 75 | |
| 76 | ## Part 3: 命名规范 |
| 77 | |
| 78 | ### 3.1 文件命名 |
| 79 | |
| 80 | | 类型 | 规范 | 示例 | |
| 81 | |------|------|------| |
| 82 | | Controller | `{Entity}Controller.cs` | `UserController.cs` | |
| 83 | | Service 接口 | `I{Entity}Service.cs` | `IUserService.cs` | |
| 84 | | Service 实现 | `{Entity}Service.cs` | `UserService.cs` | |
| 85 | | Entity | PascalCase | `SysUser.cs` | |
| 86 | | DTO | 功能 + 类型后缀 | `UserForm.cs`, `UserVO.cs` | |
| 87 | | Enums | PascalCase + 无后缀 | `ActionType.cs` | |
| 88 | |
| 89 | ### 3.2 类命名 |
| 90 | |
| 91 | | 类型 | 规范 | 示例 | |
| 92 | |------|------|------| |
| 93 | | Controller | `{Entity}Controller` | `UserController` | |
| 94 | | Service 接口 | `I{Entity}Service` | `IUserService` | |
| 95 | | Service 实现 | `{Entity}Service` | `UserService` | |
| 96 | | Entity | PascalCase | `SysUser` | |
| 97 | | DTO | 功能 + 类型 | `UserVO`, `UserForm`, `UserPageQuery` | |
| 98 | |
| 99 | **DTO 后缀规范**: |
| 100 | |
| 101 | | 后缀 | 用途 | 示例 | |
| 102 | |------|------|------| |
| 103 | | `VO` | 视图对象(返回前端) | `UserVO`, `RolePageVO` | |
| 104 | | `Form` / `RequestDto` | 表单/请求对象 | `UserForm`, `LoginRequestDto` | |
| 105 | | `Query` | 查询参数 | `UserQuery`, `RolePageQuery` | |
| 106 | | `Dto` | 传输对象 | `CurrentUserDto`, `ExcelResultDto` | |
| 107 | |
| 108 | ### 3.3 方法命名 |
| 109 | |
| 110 | | 动作 | 前缀 | 示例 | |
| 111 | |------|------|------| |
| 112 | | 查询单个 | Get | `GetByIdAsync()` | |
| 113 | | 查询列表 | List | `ListUsersAsync()` | |
| 114 | | 分页查询 | Page | `PageUsersAsync()` | |
| 115 | | 新增 | Create | `CreateUserAsync()` | |
| 116 | | 更新 | Update | `UpdateUserAsync()` | |
| 117 | | 删除 | Delete | `DeleteUserAsync()` | |
| 118 | | 下拉选项 | GetXxxOptions | `GetUserOptionsAsync()` | |
| 119 | |
| 120 | ### 3.4 变量命名 |
| 121 | |
| 122 | | 类型 | 规范 | 示例 | |
| 123 | |------|------|------| |
| 124 | | 局部变量 | camelCase | `userList` | |
| 125 | | 常量 | PascalCase | `MaxSize` | |
| 126 | | 私有字段 | `_` 前缀 camelCase | `_userService` | |
| 127 | | 布尔值 | Is/Has/Can 前缀 | `IsDeleted`, `HasPermission` | |
| 128 | |
| 129 | > ⚠️ ASP.NET Core 约定私有字段用 `_` 前缀(与 Spring Boot 不同),与官方文档和 EF Core 一致。 |
| 130 | |
| 131 | --- |
| 132 | |
| 133 | ## Part 4: RESTful API 规范 |
| 134 | |
| 135 | ### 4.1 标准 CRUD 路径 |
| 136 | |
| 137 | | 操作 | 方法 | 路径 | 说明 | |
| 138 | |------|------|------|------| |
| 139 | | 分页列表 | `GET` | `/api/v1/users/page` | Query 参数 | |
| 140 | | 详情 | `GET` | `/api/v1/users/{id}` | 路径参数 | |
| 141 | | 表单数据 | `GET` | `/api/v1/users/{id}/form` | 编辑回显 | |
| 142 | | 新增 | `POST` | `/api/v1/users` | Body: `UserForm` | |
| 143 | | 更新 | `PUT` | `/api/v1/users` | Body: `UserForm` | |
| 144 | | 删除 | `DELETE` | `/api/v1/users/{id}` | 路径参数 | |
| 145 | | 批量删除 | `DELETE` | `/api/v1/users/batch` | Body: `List<long>` | |
| 146 | | 下拉选项 | `GET` | `/api/v1/users/options` | |