template-instantiation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTemplate Instantiation
模板实例化
This skill creates .NET projects from templates using CLI commands, with guidance for parameter validation, Central Package Management adaptation, and multi-project composition.
dotnet new本Skill通过 CLI命令从模板创建.NET项目,提供参数验证、中央包管理(CPM)适配以及多项目组合的指导。
dotnet newWhen to Use
适用场景
- User asks to create a new .NET project, app, or service
- User needs a solution with multiple projects (API + tests + library)
- User wants to create a project that respects existing
Directory.Packages.props - User needs to install or manage template packages
- 用户请求创建新的.NET项目、应用或服务
- 用户需要包含多个项目的解决方案(API + 测试 + 类库)
- 用户希望创建遵循现有的项目
Directory.Packages.props - 用户需要安装或管理模板包
When Not to Use
不适用场景
- User is searching for or comparing templates — route to skill
template-discovery - User wants to author a custom template — route to skill
template-authoring - User wants to add packages to an existing project — use directly
dotnet add package
- 用户正在搜索或对比模板 — 转至Skill
template-discovery - 用户想要创作自定义模板 — 转至Skill
template-authoring - 用户想要向现有项目添加包 — 直接使用
dotnet add package
Inputs
输入参数
| Input | Required | Description |
|---|---|---|
| Template name or intent | Yes | Template short name (e.g., |
| Project name | Yes | Name for the created project |
| Output path | Recommended | Directory where the project should be created |
| Parameters | No | Template-specific parameters (e.g., |
| 输入项 | 是否必填 | 描述 |
|---|---|---|
| 模板名称或意图 | 是 | 模板简称(例如: |
| 项目名称 | 是 | 待创建项目的名称 |
| 输出路径 | 推荐 | 项目创建的目标目录 |
| 参数 | 否 | 模板特定参数(例如: |
Workflow
工作流程
Step 1: Resolve template and parameters
步骤1:解析模板和参数
If the user provides a natural-language description, map it to a template short name (see the keyword table in the skill). If they provide a template name, proceed directly.
template-discoveryUse to review available parameters, defaults, and types for any parameters the user did not specify.
dotnet new <template> --help如果用户提供自然语言描述,将其映射为模板简称(参考 Skill中的关键字表)。如果用户已提供模板名称,则直接进入下一步。
template-discovery使用查看用户未指定的参数的可用选项、默认值和类型。
dotnet new <template> --helpStep 2: Analyze the workspace
步骤2:分析工作区
Check the existing solution structure before creating:
- Is Central Package Management (CPM) enabled? Look for
Directory.Packages.props - What target frameworks are in use? Check existing files
.csproj - Is there a pinning the SDK?
global.json
This ensures the new project is consistent with the workspace.
创建前检查现有解决方案结构:
- 是否启用了中央包管理(CPM)?查找文件
Directory.Packages.props - 当前使用的目标框架是什么?检查现有文件
.csproj - 是否存在固定了SDK版本?
global.json
这确保新项目与工作区保持一致。
Step 3: Preview the creation
步骤3:预览创建过程
Use to show the user what files would be created. Confirm before proceeding.
dotnet new <template> --dry-runbash
dotnet new webapi --name MyApi --framework net10.0 --dry-run使用向用户展示将创建的文件,确认后再继续。
dotnet new <template> --dry-runbash
dotnet new webapi --name MyApi --framework net10.0 --dry-runStep 4: Create the project
步骤4:创建项目
Use with the template name and all parameters:
dotnet newbash
dotnet new webapi --name MyApi --output ./src/MyApi --framework net10.0 --auth Individual使用命令搭配模板名称和所有参数:
dotnet newbash
dotnet new webapi --name MyApi --output ./src/MyApi --framework net10.0 --auth IndividualCommon parameter combinations
常见参数组合
| Template | Parameters | Example |
|---|---|---|
| | |
| | |
| | |
| | |
| | |
Note: Use to see all available parameters for any template.
dotnet new <template> --helpAfter creation, if the workspace uses CPM:
- Check for inline
.csprojversions<PackageReference> - Move version attributes to as
Directory.Packages.propsentries<PackageVersion> - Remove attributes from the
Version.csproj
| 模板 | 参数 | 示例 |
|---|---|---|
| | |
| | |
| | |
| | |
| | |
注意:使用查看任意模板的所有可用参数。
dotnet new <template> --help创建完成后,如果工作区使用CPM:
- 检查文件中的内联
.csproj版本<PackageReference> - 将版本属性移至中作为
Directory.Packages.props条目<PackageVersion> - 从中移除
.csproj属性Version
Step 5: Multi-project composition (optional)
步骤5:多项目组合(可选)
For complex structures, create each project sequentially and wire them together:
bash
dotnet new webapi --name MyApi --output ./src/MyApi
dotnet new xunit --name MyApi.Tests --output ./tests/MyApi.Tests
dotnet add ./tests/MyApi.Tests reference ./src/MyApi
dotnet sln add ./src/MyApi ./tests/MyApi.Tests对于复杂结构,依次创建每个项目并将它们关联起来:
bash
dotnet new webapi --name MyApi --output ./src/MyApi
dotnet new xunit --name MyApi.Tests --output ./tests/MyApi.Tests
dotnet add ./tests/MyApi.Tests reference ./src/MyApi
dotnet sln add ./src/MyApi ./tests/MyApi.TestsStep 6: Template package management
步骤6:模板包管理
Install or uninstall template packages:
bash
dotnet new install Microsoft.DotNet.Web.ProjectTemplates.10.0
dotnet new uninstall Microsoft.DotNet.Web.ProjectTemplates.10.0安装或卸载模板包:
bash
dotnet new install Microsoft.DotNet.Web.ProjectTemplates.10.0
dotnet new uninstall Microsoft.DotNet.Web.ProjectTemplates.10.0Step 7: Post-creation verification
步骤7:创建后验证
- Verify the project builds:
dotnet build - If added to a solution, verify at the solution level
dotnet build - If CPM was adapted, verify has the new entries
Directory.Packages.props
- 验证项目是否构建成功:
dotnet build - 如果已添加到解决方案,验证在解决方案级别执行是否成功
dotnet build - 如果适配了CPM,验证是否包含新条目
Directory.Packages.props
Validation
验证清单
- Project was created successfully with the expected files
- Project builds cleanly with
dotnet build - If CPM is active, has no version attributes and
.csprojhas matching entriesDirectory.Packages.props - Package versions in the project are current (not stale template defaults)
- If multi-project, all projects build and reference each other correctly
- 项目已成功创建,包含预期文件
- 项目通过可干净构建
dotnet build - 如果CPM处于激活状态,无版本属性且
.csproj有匹配条目Directory.Packages.props - 项目中的包版本为当前最新版本(而非过时的模板默认值)
- 如果是多项目结构,所有项目均可构建且引用关系正确
Common Pitfalls
常见陷阱
| Pitfall | Solution |
|---|---|
| Not checking for CPM before creating a project | If |
| Creating projects without specifying the framework | Always specify |
| Not adding the project to the solution | After creation, run |
| Not verifying the project builds | Always run |
| 陷阱 | 解决方案 |
|---|---|
| 创建项目前未检查CPM | 如果存在 |
| 创建项目时未指定框架 | 当模板支持多个目标框架(TFM)时,务必指定 |
| 未将项目添加到解决方案 | 创建完成后,运行 |
| 未验证项目是否可构建 | 创建后务必运行 |
More Info
更多信息
- Central Package Management — CPM documentation
- dotnet new — CLI reference
- Central Package Management — CPM官方文档
- dotnet new — CLI参考文档