@moon791017/neo-skills 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GEMINI.md +115 -0
- package/README.md +155 -0
- package/bin/install-claude-skills.js +72 -0
- package/commands/neo/cd-app-service.toml +20 -0
- package/commands/neo/cd-iis.toml +20 -0
- package/commands/neo/ci-dotnet.toml +21 -0
- package/commands/neo/clarification.toml +48 -0
- package/commands/neo/code-review.toml +33 -0
- package/commands/neo/dotnet-gen-interface.toml +31 -0
- package/commands/neo/explain.toml +44 -0
- package/commands/neo/git-commit.toml +49 -0
- package/dist/hooks/secret-guard.js +2 -0
- package/dist/server.js +220 -0
- package/gemini-extension.json +15 -0
- package/package.json +39 -0
- package/skills/azure-pipelines/SKILL.md +45 -0
- package/skills/azure-pipelines/templates/build/build-dotnet.yml +92 -0
- package/skills/azure-pipelines/templates/deploy/deploy-app-service.yml +71 -0
- package/skills/azure-pipelines/templates/deploy/deploy-iis.yml +189 -0
- package/skills/azure-pipelines/templates/util/clean-artifact.yml +40 -0
- package/skills/azure-pipelines/templates/util/extract-artifact.yml +57 -0
- package/skills/azure-pipelines/templates/util/iis/iis-backup.yml +92 -0
- package/skills/azure-pipelines/templates/util/iis/iis-deploy-files.yml +112 -0
- package/skills/azure-pipelines/templates/util/iis/iis-manage-website.yml +112 -0
- package/skills/azure-pipelines/templates/util/iis/iis-rollback.yml +98 -0
- package/skills/azure-pipelines/templates/util/iis/iis-start-website.yml +89 -0
- package/skills/azure-pipelines/templates/util/iis/iis-stop-website.yml +80 -0
- package/skills/azure-pipelines/templates/util/iis/iis-task.yml +157 -0
- package/skills/azure-pipelines/templates/util/set-aspnetcore-env.yml +77 -0
- package/skills/clarification/SKILL.md +22 -0
- package/skills/code-review/SKILL.md +72 -0
- package/skills/csharp/SKILL.md +87 -0
- package/skills/csharp/reference/anti-patterns.md +142 -0
- package/skills/csharp/reference/coding-style.md +86 -0
- package/skills/csharp/reference/patterns.md +142 -0
- package/skills/csharp-interface-generator/SKILL.md +40 -0
- package/skills/dotnet/SKILL.md +41 -0
- package/skills/dotnet-ef-core/SKILL.md +78 -0
- package/skills/dotnet-ef-core/reference/anti-patterns.md +51 -0
- package/skills/dotnet-ef-core/reference/coding-style.md +42 -0
- package/skills/dotnet-ef-core/reference/patterns.md +53 -0
- package/skills/dotnet-minimal-apis/SKILL.md +78 -0
- package/skills/dotnet-minimal-apis/reference/anti-patterns.md +59 -0
- package/skills/dotnet-minimal-apis/reference/coding-style.md +54 -0
- package/skills/dotnet-minimal-apis/reference/patterns.md +68 -0
- package/skills/dotnet-mvc/SKILL.md +78 -0
- package/skills/dotnet-mvc/reference/anti-patterns.md +49 -0
- package/skills/dotnet-mvc/reference/coding-style.md +43 -0
- package/skills/dotnet-mvc/reference/patterns.md +56 -0
- package/skills/dotnet-webapi/SKILL.md +78 -0
- package/skills/dotnet-webapi/reference/anti-patterns.md +48 -0
- package/skills/dotnet-webapi/reference/coding-style.md +47 -0
- package/skills/dotnet-webapi/reference/patterns.md +52 -0
- package/skills/explain/SKILL.md +27 -0
- package/skills/git-commit/SKILL.md +84 -0
- package/skills/python/SKILL.md +61 -0
- package/skills/python/reference/anti-patterns.md +177 -0
- package/skills/python/reference/coding-style.md +92 -0
- package/skills/python/reference/patterns.md +112 -0
- package/skills/python-manager/SKILL.md +61 -0
- package/skills/start-plan/SKILL.md +29 -0
- package/skills/swift/SKILL.md +78 -0
- package/skills/swift/reference/anti-patterns.md +75 -0
- package/skills/swift/reference/coding-style.md +56 -0
- package/skills/swift/reference/patterns.md +94 -0
- package/skills/swift-ui/SKILL.md +76 -0
- package/skills/swift-ui/reference/anti-patterns.md +52 -0
- package/skills/swift-ui/reference/coding-style.md +46 -0
- package/skills/swift-ui/reference/patterns.md +87 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: dotnet
|
|
3
|
+
version: "1.0.0"
|
|
4
|
+
category: "Framework"
|
|
5
|
+
description: 用於廣泛 .NET 工作的核心路由技能。此技能將偵測本機安裝的 .NET SDK 版本(要求 .NET 6+),並根據使用者的問題,將任務引導或準備載入其他合適的子技能(例如 Minimal APIs, MVC, Web API, EF Core)。
|
|
6
|
+
compatibility: "Supports .NET 6.0 and above. Requires .NET SDK installed on the local machine."
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# .NET Core Routing Skill
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
This skill serves as the unified entry point for the .NET development environment. It ensures the system environment meets the requirements for modern .NET development, and has the capability to analyze user requirements, generate corresponding command-line operations, and route to the appropriate domain expert skills.
|
|
13
|
+
|
|
14
|
+
## Core Responsibilities (Perceive & Act)
|
|
15
|
+
|
|
16
|
+
1. **Environment Detection and Version Validation**
|
|
17
|
+
- Run `dotnet --version` to check the currently installed SDK version.
|
|
18
|
+
- **Mandatory requirement**: Only **.NET 6 or higher** is supported. If a version lower than .NET 6 is detected, prompt the user to upgrade.
|
|
19
|
+
|
|
20
|
+
2. **Execute Appropriate .NET CLI**
|
|
21
|
+
- Provide or execute modern CLI commands based on the detected version and project state.
|
|
22
|
+
- Examples:
|
|
23
|
+
- Build and run: `dotnet build`, `dotnet run`
|
|
24
|
+
- Test: `dotnet test`
|
|
25
|
+
- Package management: `dotnet add package`, `dotnet tool restore`
|
|
26
|
+
|
|
27
|
+
3. **Requirement Analysis and Dynamic Skill Routing**
|
|
28
|
+
- Determine the involved domain based on the user's specific problem or project architecture.
|
|
29
|
+
- If the user's task falls into the following specific domains, please **prepare to load or suggest the user switch to** the corresponding sub-skill (please note that these sub-skills may not be implemented yet; inform the user of the future implementation direction):
|
|
30
|
+
- **`dotnet-minimal-apis`**: When the requirement is for lightweight routing development, high-performance microservices, and Controllers are not used.
|
|
31
|
+
- **`dotnet-webapi`**: When the requirement is for traditional RESTful API development following the controller pattern.
|
|
32
|
+
- **`dotnet-mvc`**: When the requirement involves Server-Side Rendering (SSR), Razor views, and ViewModels.
|
|
33
|
+
- **`dotnet-ef-core`**: When the requirement involves database migrations, Linq query optimization, or model mapping.
|
|
34
|
+
|
|
35
|
+
## Usage Guidelines
|
|
36
|
+
When this skill is triggered, please prioritize checking the SDK environment, then answer the user's .NET-related questions. If the problem is found to belong to one of the four professional domains mentioned above, proactively suggest and prepare to delegate it to the corresponding expert skill for processing.
|
|
37
|
+
|
|
38
|
+
## Documentation
|
|
39
|
+
|
|
40
|
+
### Official References
|
|
41
|
+
- [.NET CLI Tools Overview](https://learn.microsoft.com/en-us/dotnet/core/tools/)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: dotnet-ef-core
|
|
3
|
+
version: "1.0.0"
|
|
4
|
+
category: "Framework"
|
|
5
|
+
description: "Entity Framework Core (EF Core) 專家指引。支援從 .NET 6 (LTS) 到 .NET 10 (LTS) 的現代開發模式,涵蓋 Code-First 移轉、查詢優化、陰影屬性與複雜資料建模。"
|
|
6
|
+
compatibility: "Supports .NET 6.0 through 10.0 environments. Compatible with SQL Server, PostgreSQL, MySQL, and SQLite."
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# EF Core Expert Skill
|
|
10
|
+
|
|
11
|
+
## Trigger On
|
|
12
|
+
- The user requests database modeling, writing Linq queries, or handling database migrations.
|
|
13
|
+
- The project contains a `DbContext` class or references the `Microsoft.EntityFrameworkCore` package.
|
|
14
|
+
- There is a need to optimize database access performance or solve N+1 query problems.
|
|
15
|
+
- The task involves complex data relationship design (one-to-many, many-to-many, inheritance mapping).
|
|
16
|
+
|
|
17
|
+
## Workflow
|
|
18
|
+
1. **Perceive (Model Awareness):**
|
|
19
|
+
- Check `.csproj` to identify the EF Core version and database provider.
|
|
20
|
+
- Analyze `DbContext` and entity configurations to confirm whether the Fluent API pattern is adopted.
|
|
21
|
+
- Identify the current state of Migrations.
|
|
22
|
+
2. **Reason (Planning Phase):**
|
|
23
|
+
- Evaluate whether queries need `.AsNoTracking()` or `.AsSplitQuery()`.
|
|
24
|
+
- Determine whether custom interceptors are needed to handle audit fields.
|
|
25
|
+
- Evaluate whether to use `ExecuteUpdate/Delete` (.NET 7+) for massive data updates.
|
|
26
|
+
3. **Act (Execution Phase):**
|
|
27
|
+
- Write efficient and safe Linq queries.
|
|
28
|
+
- Implement entity configuration classes (`IEntityTypeConfiguration`).
|
|
29
|
+
- Create and execute database migration commands.
|
|
30
|
+
4. **Validate (Standard Validation):**
|
|
31
|
+
- Check if the generated SQL meets performance expectations (avoiding full table scans).
|
|
32
|
+
- Validate concurrency conflict handling logic.
|
|
33
|
+
- Ensure all database accesses follow asynchronous patterns.
|
|
34
|
+
|
|
35
|
+
## Feature Roadmap (.NET 6 - 10)
|
|
36
|
+
|
|
37
|
+
### .NET 6 & 7 (Foundation)
|
|
38
|
+
- **Split Queries**: Solve the Cartesian product problem.
|
|
39
|
+
- **Bulk Operations**: Use `ExecuteUpdate/Delete` for high-performance updates.
|
|
40
|
+
- **JSON Columns**: Built-in JSON mapping support.
|
|
41
|
+
- **Compiled Models**: Reduce startup time for large models.
|
|
42
|
+
|
|
43
|
+
### .NET 8 & 9 (Productivity)
|
|
44
|
+
- **Primitive Collections**: Use simple type collections in queries.
|
|
45
|
+
- **HierarchyId**: Support for SQL Server hierarchical data.
|
|
46
|
+
- **Auto-compiled Queries**: Further optimize the query compilation process.
|
|
47
|
+
- **Complex Types**: Better support for Value Objects.
|
|
48
|
+
|
|
49
|
+
### .NET 10+ (Cutting Edge)
|
|
50
|
+
- **Advanced Interceptors**: More query lifecycle hooks.
|
|
51
|
+
- **Optimized AOT Support**: AOT pre-compilation optimization for cloud-native environments.
|
|
52
|
+
|
|
53
|
+
## Coding Standards
|
|
54
|
+
- **Fluent API Preference**: Prioritize entity configuration classes over Data Annotations.
|
|
55
|
+
- **Async Always**: All I/O operations must be asynchronous.
|
|
56
|
+
- **No-Tracking by Default**: Read-only queries must always use `.AsNoTracking()`.
|
|
57
|
+
- **Naming**: Entity names correspond to tables, foreign key names have clear semantics.
|
|
58
|
+
|
|
59
|
+
## Deliver
|
|
60
|
+
- **Optimized Linq Queries**: Provide query code with the potential for high-performance SQL translation.
|
|
61
|
+
- **Migration Scripts & Guidance**: Provide correct migration commands and manual adjustment suggestions.
|
|
62
|
+
- **Database Schema Design**: Design normalized entity relationships based on business requirements.
|
|
63
|
+
|
|
64
|
+
## Validate
|
|
65
|
+
- Ensure the provided code complies with EF Core performance best practices.
|
|
66
|
+
- Validate whether the code correctly handles Nulls and database constraints.
|
|
67
|
+
- Confirm database connection and resource release logic is correct (using `using` or Scoped DI).
|
|
68
|
+
|
|
69
|
+
## Documentation
|
|
70
|
+
### Official References
|
|
71
|
+
- [Entity Framework Core Overview](https://learn.microsoft.com/en-us/ef/core/)
|
|
72
|
+
- [Performance Optimization in EF Core](https://learn.microsoft.com/en-us/ef/core/performance/)
|
|
73
|
+
- [Supported Database Providers](https://learn.microsoft.com/en-us/ef/core/providers/)
|
|
74
|
+
|
|
75
|
+
### Internal References
|
|
76
|
+
- [EF Core Coding Style and Naming Conventions](reference/coding-style.md)
|
|
77
|
+
- [EF Core Anti-Patterns and Best Practices](reference/anti-patterns.md)
|
|
78
|
+
- [EF Core Modern Patterns Guide](reference/patterns.md)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# EF Core Anti-Patterns & Best Practices
|
|
2
|
+
|
|
3
|
+
This document lists common mistakes in Entity Framework Core development and their corresponding correct practices.
|
|
4
|
+
|
|
5
|
+
## 1. Performance and Querying
|
|
6
|
+
|
|
7
|
+
### 1.1 Avoid N+1 Query Problems
|
|
8
|
+
**Problem**: Accessing navigation properties inside a loop, causing an excessive amount of SQL queries to be executed.
|
|
9
|
+
|
|
10
|
+
- **Bad**: Iterating over Users and accessing `user.Orders` inside the loop without pre-loading.
|
|
11
|
+
- **Good**: Use `.Include(u => u.Orders)` for Eager Loading, or use a dedicated projection.
|
|
12
|
+
|
|
13
|
+
### 1.2 Avoid Complex C# Functions in Queries
|
|
14
|
+
**Problem**: EF Core cannot translate custom C# methods into SQL, forcing the query to be evaluated on the client-side (In-Memory).
|
|
15
|
+
|
|
16
|
+
- **Bad**: `Where(u => MyCustomLogic(u.Name))`.
|
|
17
|
+
- **Good**: Use built-in SQL functions or methods provided by `EF.Functions` as much as possible.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 2. Lifecycle Management
|
|
22
|
+
|
|
23
|
+
### 2.1 Avoid Sharing DbContext
|
|
24
|
+
**Problem**: `DbContext` is not thread-safe. Sharing the same instance across multiple threads or concurrent requests will lead to data corruption or exceptions.
|
|
25
|
+
|
|
26
|
+
- **Bad**: Storing `DbContext` in a static variable or a singleton service.
|
|
27
|
+
- **Good**: Register `DbContext` with a `Scoped` lifecycle (the default in ASP.NET Core).
|
|
28
|
+
|
|
29
|
+
### 2.2 Avoid Long Lifecycles
|
|
30
|
+
- **Problem**: Over time, the DbContext's Change Tracker accumulates a massive amount of entities, making queries and `SaveChanges` increasingly slow.
|
|
31
|
+
- **Good**: For large batch tasks, you should periodically dispose of the old Context and create a new instance.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 3. Model Definition
|
|
36
|
+
|
|
37
|
+
### 3.1 Avoid Over-relying on Data Annotations
|
|
38
|
+
- **Problem**: Entity classes become polluted with database configuration attributes (like `[Table]`, `[Column]`), violating the Single Responsibility Principle.
|
|
39
|
+
- **Good**: Use Fluent API (`IEntityTypeConfiguration`) to maintain the purity of entities.
|
|
40
|
+
|
|
41
|
+
### 3.2 Avoid Ignoring Concurrency Conflicts
|
|
42
|
+
- **Bad**: When multiple users modify the same data simultaneously, the last one directly overwrites previous modifications.
|
|
43
|
+
- **Good**: Add a `RowVersion` or `Timestamp` column to critical entities to implement optimistic concurrency control.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 4. Tooling and Development
|
|
48
|
+
|
|
49
|
+
### 4.1 Avoid Decoupling the Development Database from Migration Scripts
|
|
50
|
+
- **Problem**: Directly modifying the database schema without creating Migrations.
|
|
51
|
+
- **Good**: Always use `dotnet ef migrations add` to manage schema changes, ensuring team environment consistency.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# EF Core Coding Conventions
|
|
2
|
+
|
|
3
|
+
This guide aims to improve the readability and maintainability of Entity Framework Core code, ensuring a high degree of consistency in database model definitions.
|
|
4
|
+
|
|
5
|
+
## 1. Entities and Modeling
|
|
6
|
+
|
|
7
|
+
### 1.1 Entity Classes
|
|
8
|
+
- **PascalCase**: All entity class names must correspond to database tables and use `PascalCase` (e.g., `Order`, `ProductCategory`).
|
|
9
|
+
- **Shadow Properties**: For fields that don't need to be exposed to business logic but are required by the database (like `CreatedDate`, `LastUpdated`), shadow properties should be used.
|
|
10
|
+
|
|
11
|
+
### 1.2 Relationship Naming
|
|
12
|
+
- **Navigation Properties**: Collection types should use plural nouns (e.g., `public ICollection<Order> Orders { get; set; }`), while single entities use singular nouns.
|
|
13
|
+
- **Foreign Key Naming**: Prioritize the `<EntityName>Id` convention (e.g., `CategoryId`).
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## 2. DbContext Configuration
|
|
18
|
+
|
|
19
|
+
- **Fluent API First**: Prioritize using `IEntityTypeConfiguration<T>` classes for entity configuration over piling up code in `OnModelCreating` or using Data Annotations on entities.
|
|
20
|
+
- **Configuration Directory**: Place all configuration classes in the `Data/Configurations` directory.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## 3. Querying Conventions
|
|
25
|
+
|
|
26
|
+
- **Asynchronous Execution**: All database access operations must prioritize asynchronous methods (e.g., `ToListAsync()`, `FirstOrDefaultAsync()`).
|
|
27
|
+
- **Read-Only Queries**: For queries that do not require updates, you must explicitly call `.AsNoTracking()` to reduce memory overhead.
|
|
28
|
+
- **Specific Field Projections**: Avoid `Select *`, and use `.Select(x => new Dto { ... })` to retrieve only the required data.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 4. Migrations Management
|
|
33
|
+
|
|
34
|
+
- **Named Migrations**: Migration names must reflect the content of the change (e.g., `AddUserPhoneNumber`, `FixOrderDiscountTypo`). Random or meaningless names are prohibited.
|
|
35
|
+
- **Prevent Data Loss**: When manually editing migration scripts, you must check for any implicit deletion actions.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 5. File Organization
|
|
40
|
+
|
|
41
|
+
- **Data Directory**: Place `DbContext`, entity definitions, and configurations under `Data/` or `Infrastructure/Persistence/`.
|
|
42
|
+
- **File-scoped Namespace**: Always use File-scoped Namespaces (C# 10+).
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# EF Core Modern Patterns
|
|
2
|
+
|
|
3
|
+
This document introduces recommended development patterns in the Entity Framework Core domain for .NET 6 to 10+.
|
|
4
|
+
|
|
5
|
+
## 1. Advanced Query Patterns
|
|
6
|
+
|
|
7
|
+
### 1.1 Split Queries (.NET 6+)
|
|
8
|
+
**Recommendation**: When dealing with complex relationships involving multiple `Include()`s, use `.AsSplitQuery()` to avoid the performance explosion caused by the "Cartesian product".
|
|
9
|
+
|
|
10
|
+
### 1.2 Compiled Queries
|
|
11
|
+
**Recommendation**: For hot-path queries that are executed frequently and are parameterized, use `EF.CompileAsyncQuery` to improve query efficiency.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## 2. Interceptors and Events
|
|
16
|
+
|
|
17
|
+
### 2.1 SaveChanges Interceptor
|
|
18
|
+
**Recommendation**: Implement a `SaveChangesInterceptor` to automatically handle audit fields (like `CreatedBy`, `CreatedAt`), ensuring the separation of business logic and data persistence concerns.
|
|
19
|
+
|
|
20
|
+
### 2.2 Query Interceptors (.NET 8+)
|
|
21
|
+
**Recommendation**: Use interceptors to dynamically modify SQL before the query is executed (e.g., automatically adding a global filter).
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## 3. Advanced Data Operations
|
|
26
|
+
|
|
27
|
+
### 3.1 Batch Updates and Deletes (ExecuteUpdate / ExecuteDelete) - .NET 7+
|
|
28
|
+
**Recommendation**: For massive update or delete operations that do not require loading entities into memory, directly use `ExecuteUpdateAsync` or `ExecuteDeleteAsync`.
|
|
29
|
+
```csharp
|
|
30
|
+
await context.Orders
|
|
31
|
+
.Where(o => o.Status == "Expired")
|
|
32
|
+
.ExecuteDeleteAsync();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 3.2 Shadow Properties and JSON Mapping
|
|
36
|
+
**Recommendation**: Leverage .NET 7+ enhanced JSON support to map complex structures directly into JSON columns in the database.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 4. Environments and Tooling
|
|
41
|
+
|
|
42
|
+
### 4.1 DbContextFactory
|
|
43
|
+
**Recommendation**: In Blazor Server or long-running background services, use `IDbContextFactory<T>` to ensure the correct lifecycle of DbContext.
|
|
44
|
+
|
|
45
|
+
### 4.2 Migrations Bundles
|
|
46
|
+
**Recommendation**: Use `dotnet ef migrations bundle` to generate a self-contained executable, simplifying the database deployment process in CI/CD.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 5. C# 14+ Forward-looking Patterns
|
|
51
|
+
|
|
52
|
+
### 5.1 Extension Types for Computed Properties on Entities
|
|
53
|
+
**Recommendation**: Leverage C# 14's Extension Types to add computed properties to POCO entities without affecting the database schema.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: dotnet-minimal-apis
|
|
3
|
+
version: "1.0.0"
|
|
4
|
+
category: "Framework"
|
|
5
|
+
description: "開發高效能、輕量級 .NET Minimal API 的專家指引。支援從 .NET 6 (LTS) 到 .NET 10 (LTS) 的現代開發模式,涵蓋路由群組、端點過濾器與符合 OpenAPI 規範的強型別回應。"
|
|
6
|
+
compatibility: "Supports .NET 6.0 through 10.0 environments. Requires .NET SDK installed locally."
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# .NET Minimal APIs Expert Skill
|
|
10
|
+
|
|
11
|
+
## Trigger On
|
|
12
|
+
- The user requests to create, debug, refactor, or review .NET Minimal APIs.
|
|
13
|
+
- The project's `Program.cs` contains `WebApplication.CreateBuilder(args)` and is not configured with a Controllers directory.
|
|
14
|
+
- The target framework is .NET 6.0 (LTS) and above.
|
|
15
|
+
- There is a need to optimize API performance or simplify Web API architecture.
|
|
16
|
+
|
|
17
|
+
## Workflow
|
|
18
|
+
1. **Perceive (Architecture Awareness):**
|
|
19
|
+
- Check `.csproj` to identify `TargetFramework`.
|
|
20
|
+
- Analyze `Program.cs` to distinguish whether it uses simple endpoints or adopts a Module-based or Vertical Slice organization.
|
|
21
|
+
- Identify whether OpenAPI (Swagger) support has been configured.
|
|
22
|
+
2. **Reason (Planning Phase):**
|
|
23
|
+
- Evaluate whether "Route Groups" need to be introduced to optimize organizational structure.
|
|
24
|
+
- Determine whether to use "Endpoint Filters" to handle cross-cutting concerns (such as parameter validation).
|
|
25
|
+
- Choose the most appropriate syntax based on the .NET version (e.g., `TypedResults` in .NET 7+).
|
|
26
|
+
3. **Act (Execution Phase):**
|
|
27
|
+
- Write clean, high-performance Minimal API code, prioritizing Lambdas or extension methods.
|
|
28
|
+
- Implement strong typing and DTOs (prioritize using `record`).
|
|
29
|
+
- Integrate dependency injection, avoiding the Service Locator pattern.
|
|
30
|
+
4. **Validate (Standard Validation):**
|
|
31
|
+
- Validate whether the endpoints correctly return expected HTTP status codes.
|
|
32
|
+
- Check whether the generated OpenAPI documentation is complete.
|
|
33
|
+
- Ensure asynchronous operations correctly pass the `CancellationToken`.
|
|
34
|
+
|
|
35
|
+
## Feature Roadmap (.NET 6 - 10)
|
|
36
|
+
|
|
37
|
+
### .NET 6 & 7 (Foundation)
|
|
38
|
+
- **Minimal Hosting**: Simplified startup process and top-level statements.
|
|
39
|
+
- **Route Groups**: Use `MapGroup` to consolidate prefixes and authentication.
|
|
40
|
+
- **Endpoint Filters**: Implement interception logic within Minimal APIs.
|
|
41
|
+
- **Typed Results**: Improved unit testing and Swagger support.
|
|
42
|
+
|
|
43
|
+
### .NET 8 & 9 (Productivity)
|
|
44
|
+
- **Antiforgery**: Built-in anti-forgery token support.
|
|
45
|
+
- **Form Binding**: Support for `[FromForm]` parameter binding.
|
|
46
|
+
- **HybridCache**: High-performance multi-level caching support.
|
|
47
|
+
- **OpenAPI Improvements**: Better OpenAPI code generation.
|
|
48
|
+
|
|
49
|
+
### .NET 10+ (Cutting Edge)
|
|
50
|
+
- **Native AOT Optimization**: AOT-optimized compilation tailored for Minimal APIs.
|
|
51
|
+
- **Enhanced Middleware Patterns**: New middleware designed for lightweight architectures.
|
|
52
|
+
|
|
53
|
+
## Coding Standards
|
|
54
|
+
- **Clean Routing**: Prioritize using extension methods to modularize routes.
|
|
55
|
+
- **Strong Typing**: Prioritize using `TypedResults` and `Results<T1, T2>`.
|
|
56
|
+
- **Async Safety**: Always accept and pass down `CancellationToken`.
|
|
57
|
+
- **DI Best Practices**: Services should be defined directly in the Handler parameters.
|
|
58
|
+
|
|
59
|
+
## Deliver
|
|
60
|
+
- **Version-Optimized API Code**: Provide the most appropriate modern API code based on the target .NET version.
|
|
61
|
+
- **Modular Architecture**: Provide structural suggestions for extracting `Program.cs` routes into extension methods.
|
|
62
|
+
- **OpenAPI Configuration**: Provide comprehensive Swagger/OpenAPI configuration suggestions.
|
|
63
|
+
|
|
64
|
+
## Validate
|
|
65
|
+
- Ensure the provided code executes correctly in a .NET 6+ environment.
|
|
66
|
+
- Validate endpoint security (authentication, authorization, Antiforgery).
|
|
67
|
+
- Check if the code complies with high-performance best practices for Minimal APIs (e.g., avoiding closures, reducing allocations).
|
|
68
|
+
|
|
69
|
+
## Documentation
|
|
70
|
+
### Official References
|
|
71
|
+
- [Minimal APIs Overview](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis)
|
|
72
|
+
- [Route groups and filters](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/route-handlers)
|
|
73
|
+
- [Typed responses in Minimal APIs](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/responses)
|
|
74
|
+
|
|
75
|
+
### Internal References
|
|
76
|
+
- [Minimal APIs Coding Style and Naming Conventions](reference/coding-style.md)
|
|
77
|
+
- [Minimal APIs Anti-Patterns and Best Practices](reference/anti-patterns.md)
|
|
78
|
+
- [Minimal APIs Modern Patterns Guide](reference/patterns.md)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# .NET Minimal APIs Anti-Patterns & Best Practices
|
|
2
|
+
|
|
3
|
+
This document lists common mistakes in Minimal APIs development and their corresponding correct practices.
|
|
4
|
+
|
|
5
|
+
## 1. Routing & Organization
|
|
6
|
+
|
|
7
|
+
### 1.1 Avoid Fat `Program.cs`
|
|
8
|
+
**Problem**: Stuffing all routes, DI registrations, and middleware into `Program.cs`, leading to maintenance difficulties.
|
|
9
|
+
|
|
10
|
+
- **Bad**: Writing hundreds of lines of `app.MapGet` in a single file.
|
|
11
|
+
- **Good**: Use extension methods to split routes for different modules into separate files.
|
|
12
|
+
|
|
13
|
+
### 1.2 Avoid Ignoring Route Conflicts
|
|
14
|
+
**Problem**: Minimal APIs routing matching is extremely flexible; improper parameter definitions can lead to route conflicts.
|
|
15
|
+
|
|
16
|
+
- **Bad**: `/users/{id}` (int) and `/users/{name}` (string) existing simultaneously without constraints.
|
|
17
|
+
- **Good**: Use route constraints, e.g., `/users/{id:int}`.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 2. Response Handling
|
|
22
|
+
|
|
23
|
+
### 2.1 Avoid Overusing Anonymous Type Responses
|
|
24
|
+
**Problem**: Returning `Results.Ok(new { id = 1 })` prevents Swagger from determining the response structure.
|
|
25
|
+
|
|
26
|
+
- **Bad**: Returning anonymous objects.
|
|
27
|
+
- **Good**: Define explicit `DTO`s or `Record`s and use `TypedResults`.
|
|
28
|
+
|
|
29
|
+
### 2.2 Avoid Ignoring `TypedResults`
|
|
30
|
+
- **Problem**: `Results.Ok()` returns an `IResult`, requiring casting in unit tests to verify the content.
|
|
31
|
+
- **Good**: Use `TypedResults.Ok(data)`, which returns `Ok<T>`, allowing direct testing of the `Value` property.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 3. Performance & Async
|
|
36
|
+
|
|
37
|
+
### 3.1 Avoid Synchronous Blocking in Handlers
|
|
38
|
+
- **Problem**: Using synchronous APIs (like `File.ReadAllText`) inside Lambdas blocks the Thread Pool.
|
|
39
|
+
- **Good**: Always use `await` and asynchronous versions (like `File.ReadAllTextAsync`).
|
|
40
|
+
|
|
41
|
+
### 3.2 Forgetting to Pass `CancellationToken`
|
|
42
|
+
- **Problem**: When a user cancels a request (closes the browser), the backend continues to execute expensive queries.
|
|
43
|
+
- **Good**: Include `CancellationToken ct` in Handler parameters and pass it down to databases or HTTP clients.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 4. Dependency Injection
|
|
48
|
+
|
|
49
|
+
### 4.1 Avoid Manually Retrieving Services from `app.Services` (Service Locator)
|
|
50
|
+
- **Bad**: `var service = app.Services.GetRequiredService<IMyService>();`.
|
|
51
|
+
- **Good**: Declare services directly in the Handler parameters to have the framework auto-inject them.
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## 5. Documentation
|
|
56
|
+
|
|
57
|
+
### 5.1 Ignoring Status Code Attributes
|
|
58
|
+
- **Problem**: Swagger only displays `200 OK`, ignoring `404 NotFound` or `400 BadRequest`.
|
|
59
|
+
- **Good**: Use `.Produces<T>(StatusCodes.Status200OK)` or directly return `Results<T1, T2>`.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# .NET Minimal APIs Coding Conventions
|
|
2
|
+
|
|
3
|
+
This guide aims to improve the development efficiency of Minimal APIs and ensure a high degree of consistency in endpoint and routing logic organization.
|
|
4
|
+
|
|
5
|
+
## 1. Routing Naming and Syntax
|
|
6
|
+
|
|
7
|
+
### 1.1 Route Templates
|
|
8
|
+
- **Kebab-case**: Route paths should prioritize lowercase letters separated by hyphens (e.g., `/api/v1/user-profiles`).
|
|
9
|
+
- **Parameter Naming**: Route parameter names should match the Handler's parameter names. Uppercase or lowercase can be used, but maintaining project consistency is recommended (e.g., `/{id}` matches `int id`).
|
|
10
|
+
|
|
11
|
+
### 1.2 Route Groups
|
|
12
|
+
- **Semantic Prefixes**: For endpoints sharing the same prefix or middleware, `MapGroup` must be used for logical grouping.
|
|
13
|
+
- **Isolated Declarations**: Avoid writing massive amounts of routes directly in `Program.cs`. It is recommended to extract group routes into separate files using extension methods.
|
|
14
|
+
```csharp
|
|
15
|
+
// Recommended: Extracted into UserEndpoints.cs
|
|
16
|
+
public static class UserEndpoints
|
|
17
|
+
{
|
|
18
|
+
public static IEndpointRouteBuilder MapUserEndpoints(this IEndpointRouteBuilder routes)
|
|
19
|
+
{
|
|
20
|
+
var group = routes.MapGroup("/users");
|
|
21
|
+
group.MapGet("/", GetAllUsers);
|
|
22
|
+
return routes;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 2. Handlers
|
|
30
|
+
|
|
31
|
+
- **Anonymous Functions vs. Named Methods**: For single-line or simple logic, Lambdas can be used. If the logic exceeds 3 lines, it should be changed to static named methods or extracted into services.
|
|
32
|
+
- **Dependency Injection**: Inject services directly into the Handler parameters.
|
|
33
|
+
- **CancellationToken**: All asynchronous Handlers returning `Task` should prioritize accepting `CancellationToken` and passing it downstream.
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 3. Response Types (Results)
|
|
38
|
+
|
|
39
|
+
- **TypedResults**: To enhance OpenAPI (Swagger) static analysis capabilities, prioritize using `TypedResults` over `Results`.
|
|
40
|
+
- **Results<T1, T2>**: When an endpoint may return multiple status codes, use `Results<T1, T2>` to provide strong typing support.
|
|
41
|
+
```csharp
|
|
42
|
+
public static async Task<Results<Ok<User>, NotFound>> GetUser(int id, IUserRepository repo)
|
|
43
|
+
{
|
|
44
|
+
var user = await repo.GetByIdAsync(id);
|
|
45
|
+
return user is not null ? TypedResults.Ok(user) : TypedResults.NotFound();
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## 4. File Organization
|
|
52
|
+
|
|
53
|
+
- **Vertical Slice**: It is recommended to organize files by feature module rather than by class type (e.g., `Features/Users/UserEndpoints.cs`, `Features/Users/UserDto.cs`).
|
|
54
|
+
- **File-scoped Namespace**: Always use File-scoped Namespaces (C# 10+) to reduce nesting.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# .NET Minimal APIs Modern Patterns
|
|
2
|
+
|
|
3
|
+
This document introduces recommended development patterns in the Minimal APIs domain for .NET 6 to 10+.
|
|
4
|
+
|
|
5
|
+
## 1. Architectural Patterns
|
|
6
|
+
|
|
7
|
+
### 1.1 Module-based Configuration
|
|
8
|
+
**Recommendation**: Use extension methods to extract routing configuration from `Program.cs`, keeping the startup file clean.
|
|
9
|
+
```csharp
|
|
10
|
+
// Program.cs
|
|
11
|
+
app.MapUserEndpoints();
|
|
12
|
+
app.MapOrderEndpoints();
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### 1.2 Endpoint Filters (.NET 7+)
|
|
16
|
+
**Recommendation**: Use filters to handle cross-cutting logic (like validation, logging), instead of writing duplicate code within Handlers.
|
|
17
|
+
```csharp
|
|
18
|
+
group.MapPost("/", CreateUser)
|
|
19
|
+
.AddEndpointFilter<ValidationFilter<UserDto>>();
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## 2. Data Transfer and Validation
|
|
25
|
+
|
|
26
|
+
### 2.1 Use Record for DTOs
|
|
27
|
+
**Recommendation**: Combine with the `[AsParameters]` attribute to simplify the Handler's parameter list.
|
|
28
|
+
```csharp
|
|
29
|
+
// Use [AsParameters] to map from multiple sources
|
|
30
|
+
app.MapGet("/search", ([AsParameters] SearchCriteria criteria) => { ... });
|
|
31
|
+
|
|
32
|
+
public record SearchCriteria(string? Query, int Page = 1, int PageSize = 10);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 2.2 Typed Results (.NET 7+)
|
|
36
|
+
**Recommendation**: Use `Results<T1, T2>` to enhance Swagger's automatic documentation capabilities and the convenience of unit testing.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 3. Security and Documentation
|
|
41
|
+
|
|
42
|
+
### 3.1 OpenAPI (Swagger) Integration
|
|
43
|
+
**Recommendation**: Use `.WithOpenApi()` and `.WithName()` to explicitly mark endpoints.
|
|
44
|
+
```csharp
|
|
45
|
+
app.MapGet("/users", () => ...)
|
|
46
|
+
.WithName("GetUsers")
|
|
47
|
+
.WithOpenApi(operation =>
|
|
48
|
+
{
|
|
49
|
+
operation.Summary = "Get all users";
|
|
50
|
+
return operation;
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 3.2 Antiforgery (.NET 8+)
|
|
55
|
+
**Recommendation**: Use `.DisableAntiforgery()` or `.ValidateAntiforgery()` directly in Minimal APIs.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## 4. Performance Optimization
|
|
60
|
+
|
|
61
|
+
### 4.1 HybridCache (.NET 9+)
|
|
62
|
+
**Recommendation**: Use `HybridCache` instead of `IDistributedCache` for better serialization performance and to prevent cache stampedes.
|
|
63
|
+
|
|
64
|
+
### 4.2 Static Endpoint Optimization
|
|
65
|
+
**Recommendation**: For endpoints that do not depend on the request context, use static Lambdas to reduce closure allocations.
|
|
66
|
+
```csharp
|
|
67
|
+
app.MapGet("/health", static () => "Healthy");
|
|
68
|
+
```
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: dotnet-mvc
|
|
3
|
+
version: "1.0.0"
|
|
4
|
+
category: "Framework"
|
|
5
|
+
description: "開發 ASP.NET Core MVC 應用程式的專家指引。支援從 .NET 6 (LTS) 到 .NET 10 (LTS) 的現代開發模式,涵蓋視圖模型、標籤協助程式、視圖元件與伺服器端渲染的最佳實踐。"
|
|
6
|
+
compatibility: "Supports .NET 6.0 through 10.0 environments. Requires .NET SDK installed locally."
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# .NET MVC Expert Skill
|
|
10
|
+
|
|
11
|
+
## Trigger On
|
|
12
|
+
- The user requests to create, debug, refactor, or review ASP.NET Core MVC applications.
|
|
13
|
+
- The project contains `Views` and `Controllers` directories and inherits from `Controller` (not `ControllerBase`).
|
|
14
|
+
- The target framework is .NET 6.0 (LTS) and above.
|
|
15
|
+
- There is a need to optimize Server-Side Rendering (SSR) performance or improve view organizational structure.
|
|
16
|
+
|
|
17
|
+
## Workflow
|
|
18
|
+
1. **Perceive (Architecture Awareness):**
|
|
19
|
+
- Check `.csproj` to identify `TargetFramework`.
|
|
20
|
+
- Analyze `Views/Shared` to confirm existing configurations for layouts and partial views.
|
|
21
|
+
- Identify whether Tag Helpers and static resource versioning are enabled.
|
|
22
|
+
2. **Reason (Planning Phase):**
|
|
23
|
+
- Evaluate whether complex Partial Views need to be migrated to View Components.
|
|
24
|
+
- Determine whether custom Tag Helpers are needed to simplify UI logic.
|
|
25
|
+
- Choose an appropriate ViewModel organization method based on project scale.
|
|
26
|
+
3. **Act (Execution Phase):**
|
|
27
|
+
- Write high-quality controllers and strongly-typed views.
|
|
28
|
+
- Implement data validation (Data Annotations) and integrate them into ViewModels.
|
|
29
|
+
- Configure and use Tag Helpers to ensure the semantic correctness of forms and links.
|
|
30
|
+
4. **Validate (Standard Validation):**
|
|
31
|
+
- Validate whether page rendering conforms to the expected HTML structure.
|
|
32
|
+
- Check whether model validation correctly operates on both the frontend and backend.
|
|
33
|
+
- Ensure Antiforgery tokens are correctly applied to all state-modifying requests.
|
|
34
|
+
|
|
35
|
+
## Feature Roadmap (.NET 6 - 10)
|
|
36
|
+
|
|
37
|
+
### .NET 6 & 7 (Foundation)
|
|
38
|
+
- **Tag Helpers**: Declarative HTML attribute extensions.
|
|
39
|
+
- **View Components**: Logic-driven UI block modularization.
|
|
40
|
+
- **Cache Tag Helper**: Server-side view fragment caching.
|
|
41
|
+
- **Nullable Views**: Better Razor null safety support.
|
|
42
|
+
|
|
43
|
+
### .NET 8 & 9 (Productivity)
|
|
44
|
+
- **Blazor Integration**: Hybrid development support for MVC and Blazor components.
|
|
45
|
+
- **Antiforgery Improvements**: Global anti-forgery automatic validation optimization.
|
|
46
|
+
- **Keyed Services in Controllers**: Finer-grained dependency injection support.
|
|
47
|
+
- **Native AOT for MVC**: Metadata compilation optimization tailored for AOT.
|
|
48
|
+
|
|
49
|
+
### .NET 10+ (Cutting Edge)
|
|
50
|
+
- **Extension Members for IHtmlHelper**: Extending view helper functions.
|
|
51
|
+
- **Advanced Serialization in Views**: Performance optimization for large view models.
|
|
52
|
+
|
|
53
|
+
## Coding Standards
|
|
54
|
+
- **Strongly Typed Views**: All views must have an explicit `@model`.
|
|
55
|
+
- **ViewModel Separation**: Strictly prohibit exposing database entities directly to views.
|
|
56
|
+
- **Naming**: View models must end with `ViewModel`, action methods prioritize asynchronous patterns.
|
|
57
|
+
- **Formatting**: Follow the Allman style and use Tag Helpers instead of HTML Helpers.
|
|
58
|
+
|
|
59
|
+
## Deliver
|
|
60
|
+
- **Version-Optimized MVC Code**: Provide modernized controllers, models, and views suitable for the target version.
|
|
61
|
+
- **UI Architecture Suggestions**: Provide design suggestions for view components and Tag Helpers.
|
|
62
|
+
- **Validation Logic**: Provide a complete model validation and error prompting implementation plan.
|
|
63
|
+
|
|
64
|
+
## Validate
|
|
65
|
+
- Ensure the code complies with C# 10+ syntax standards.
|
|
66
|
+
- Validate endpoints correctly handle model validation failures and return form states.
|
|
67
|
+
- Confirm static resources and scripts are correctly cached via `asp-append-version`.
|
|
68
|
+
|
|
69
|
+
## Documentation
|
|
70
|
+
### Official References
|
|
71
|
+
- [Overview of ASP.NET Core MVC](https://learn.microsoft.com/en-us/aspnet/core/mvc/overview)
|
|
72
|
+
- [Tag Helpers in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro)
|
|
73
|
+
- [View components in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components)
|
|
74
|
+
|
|
75
|
+
### Internal References
|
|
76
|
+
- [MVC Coding Style and Naming Conventions](reference/coding-style.md)
|
|
77
|
+
- [MVC Anti-Patterns and Best Practices](reference/anti-patterns.md)
|
|
78
|
+
- [MVC Modern Patterns Guide](reference/patterns.md)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# .NET MVC Anti-Patterns & Best Practices
|
|
2
|
+
|
|
3
|
+
This document lists common mistakes in ASP.NET Core MVC development and their corresponding correct practices.
|
|
4
|
+
|
|
5
|
+
## 1. Views and Data Transfer (Views & Data)
|
|
6
|
+
|
|
7
|
+
### 1.1 Avoid Writing Too Much Logic in Views
|
|
8
|
+
**Problem**: Using massive amounts of `if`, `switch`, or `foreach` for complex calculations in `.cshtml` files.
|
|
9
|
+
|
|
10
|
+
- **Bad**: Views containing database queries or complex business rules.
|
|
11
|
+
- **Good**: All pre-display processing should be done in the Controller or ViewModel; the view should only be responsible for rendering.
|
|
12
|
+
|
|
13
|
+
### 1.2 Avoid Overusing `ViewData` or `ViewBag`
|
|
14
|
+
**Problem**: `ViewData/ViewBag` are dynamic and lack type safety, which easily leads to runtime errors.
|
|
15
|
+
|
|
16
|
+
- **Bad**: Relying on string keys to pass large amounts of data.
|
|
17
|
+
- **Good**: Always create strongly-typed `ViewModel`s and use the `@model` directive.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 2. Resource Management & Performance
|
|
22
|
+
|
|
23
|
+
### 2.1 Avoid Ignoring Static File Caching
|
|
24
|
+
- **Problem**: Redownloading the same CSS/JS every time the page is refreshed.
|
|
25
|
+
- **Good**: Correctly configure Cache-Control Headers in `app.UseStaticFiles()`.
|
|
26
|
+
|
|
27
|
+
### 2.2 Avoid Calling `@Html.Action` in Loops (Legacy Style)
|
|
28
|
+
- **Problem**: In .NET Core and later, mechanisms that trigger full sub-requests frequently should be avoided.
|
|
29
|
+
- **Good**: Switch to `ViewComponent`s to achieve higher execution efficiency.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## 3. Security
|
|
34
|
+
|
|
35
|
+
### 3.1 Avoid Disabling Antiforgery Validation
|
|
36
|
+
- **Bad**: Marking `[IgnoreAntiforgeryToken]` on POST actions to save effort.
|
|
37
|
+
- **Good**: Enable Antiforgery token validation globally and ensure all forms contain the Hidden Token.
|
|
38
|
+
|
|
39
|
+
### 3.2 Avoid Directly Returning HTML Strings
|
|
40
|
+
- **Problem**: Returning HTML code directly from the Controller.
|
|
41
|
+
- **Good**: Always use Razor views to let the system automatically handle HTML escaping, preventing XSS attacks.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## 4. Development Practices
|
|
46
|
+
|
|
47
|
+
### 4.1 Avoid Ignoring Model Validation State
|
|
48
|
+
- **Bad**: Processing data directly in POST actions without checking `if (!ModelState.IsValid)`.
|
|
49
|
+
- **Good**: Check the validation state first. If it fails, redirect back to the form page and display error messages.
|