Template Authoring
This skill helps an agent create and validate custom dotnet new templates. It guides bootstrapping templates from existing projects and validates template.json files for authoring issues before publishing.
When to Use
- User wants to create a reusable template from an existing .csproj
- User wants to validate a template.json for correctness
- User is setting up
.template.config/template.json from scratch
- User wants to package a template for NuGet distribution
When Not to Use
- User wants to find or use existing templates — route to
template-discovery or template-instantiation
- User has MSBuild issues unrelated to template authoring — route to
dotnet-msbuild plugin
Inputs
| Input |
Required |
Description |
| Source project path |
For creation |
Path to the .csproj to use as template source |
| template.json path |
For validation |
Path to an existing template.json to validate |
| Template name |
For creation |
Human-readable name for the template |
| Short name |
Recommended |
Short name for dotnet new <shortname> usage |
Workflow
Step 1: Bootstrap from existing project
Analyze the source .csproj and create a .template.config/template.json that preserves the project's conventions:
- Create the
.template.config directory next to the project
- Generate
template.json with:
identity in reverse-DNS format (e.g., MyOrg.Templates.MyLib)
name as the human-readable template name
shortName for dotnet new <shortname> usage
sourceName set to the project name (enables name replacement)
classifications for discoverability (e.g., ["Library"])
tags with language and type
Example generated template.json:
{
"$schema": "http://json.schemastore.org/template",
"author": "MyOrg",
"classifications": ["Library"],
"identity": "MyOrg.Templates.MyLib",
"name": "My Library Template",
"shortName": "mylib",
"sourceName": "MyLib",
"tags": {
"language": "C#",
"type": "project"
},
"symbols": {
"Framework": {
"type": "parameter",
"datatype": "choice",
"defaultValue": "net9.0",
"choices": [
{ "choice": "net9.0" },
{ "choice": "net10.0" }
],
"replaces": "net9.0"
}
}
}
Preserve from the source project:
- SDK type, package references with metadata (PrivateAssets, IncludeAssets)
- Properties (OutputType, TreatWarningsAsErrors)
- Central Package Management and shared compile patterns
Step 2: Validate template.json
Read and review the template.json for common authoring issues:
Validation checks to perform:
- Required fields — verify
identity, name, and shortName are present
- Identity format — use reverse-DNS format (e.g.,
MyOrg.Templates.WebApi)
- Parameter issues — check datatypes are valid (
string, bool, choice, int, float), choices have defaults, descriptions are present
- ShortName conflicts — avoid names that collide with built-in CLI commands (
build, run, test, publish). Check with dotnet new list to see if the name is already taken
- Post-action completeness — verify post-actions have all required configuration
- Tags — ensure language, type, and classification tags are set for discoverability
Step 3: Refine the template
Based on validation results and user requirements:
- Add parameters with appropriate types (string, bool, choice), defaults, and descriptions
- Add conditional content using
#if preprocessor directives for optional features
- Configure post-actions for solution add, restore, or custom scripts
- Set constraints to restrict which SDKs or workloads the template supports
- Add classifications and tags for discoverability
Step 4: Test the template locally
- Install the template from the local directory:
dotnet new install ./path/to/template/root
- Run a dry-run to verify the output:
dotnet new mylib --name TestProject --dry-run
- Create a test project and verify it builds:
dotnet new mylib --name TestProject --output ./test-output
dotnet build ./test-output/TestProject
- Verify all parameters produce the expected output
Step 5: Package for distribution
- Create a
.nuspec or use <PackAsTool> in a packaging .csproj
- Include the template directory with
.template.config/template.json
- Run
dotnet pack to create the .nupkg
- Test installation from the
.nupkg:dotnet new install ./path/to/package.nupkg
Validation
Common Pitfalls
| Pitfall |
Solution |
| Identity format issues |
Use reverse-DNS format (e.g., MyOrg.Templates.WebApi). Avoid spaces or special characters. |
| ShortName conflicts with CLI commands |
Avoid names like build, run, test, publish. Check by running dotnet new list to see if the name is already taken. |
| Missing parameter descriptions |
Every parameter should have a description and displayName for discoverability. |
| Not testing all parameter combinations |
Use dotnet new <template> --dry-run with different parameter values to verify conditional content works correctly. |
| Hardcoded versions in template |
Use sourceName replacement for project names and consider parameterizing framework versions. |
| Not setting classifications |
Add appropriate classifications (e.g., ["Web", "API"]) for template discovery. |
More Info
1---2name: template-authoring3description: Guides creation and validation of custom dotnet new templates. Generates templates from existing projects and validates template.json for authoring issues. USE FOR: creating a reusable dotnet new template from an existing project, validating template.json files for schema compliance and parameter issues, bootstrapping .template.config/template.json with correct identity, shortName, parameters, and post-actions, packaging templates as NuGet packages for distribution. DO NOT USE FOR: finding or using existing templates (use template-discovery and template-instantiation), MSBuild project file issues unrelated to template authoring, NuGet package publishing (only template packaging structure).4---5
6# Template Authoring
7
8This skill helps an agent create and validate custom `dotnet new` templates. It guides bootstrapping templates from existing projects and validates `template.json` files for authoring issues before publishing.
9
10## When to Use
11
12- User wants to create a reusable template from an existing .csproj
13- User wants to validate a template.json for correctness
14- User is setting up `.template.config/template.json` from scratch
15- User wants to package a template for NuGet distribution
16
17## When Not to Use
18
19- User wants to find or use existing templates — route to `template-discovery` or `template-instantiation`
20- User has MSBuild issues unrelated to template authoring — route to `dotnet-msbuild` plugin
21
22## Inputs
23
24| Input | Required | Description |
25|-------|----------|-------------|
26| Source project path | For creation | Path to the .csproj to use as template source |
27| template.json path | For validation | Path to an existing template.json to validate |
28| Template name | For creation | Human-readable name for the template |
29| Short name | Recommended | Short name for `dotnet new <shortname>` usage |
30
31## Workflow
32
33### Step 1: Bootstrap from existing project
34
35Analyze the source `.csproj` and create a `.template.config/template.json` that preserves the project's conventions:
36
371. Create the `.template.config` directory next to the project
382. Generate `template.json` with:
39 - `identity` in reverse-DNS format (e.g., `MyOrg.Templates.MyLib`)
40 - `name` as the human-readable template name
41 - `shortName` for `dotnet new <shortname>` usage
42 - `sourceName` set to the project name (enables name replacement)
43 - `classifications` for discoverability (e.g., `["Library"]`)
44 - `tags` with language and type
45
46Example generated `template.json`:
47```json
48{
49 "$schema": "http://json.schemastore.org/template",
50 "author": "MyOrg",
51 "classifications": ["Library"],
52 "identity": "MyOrg.Templates.MyLib",
53 "name": "My Library Template",
54 "shortName": "mylib",
55 "sourceName": "MyLib",
56 "tags": {
57 "language": "C#",
58 "type": "project"
59 },
60 "symbols": {
61 "Framework": {
62 "type": "parameter",
63 "datatype": "choice",
64 "defaultValue": "net9.0",
65 "choices": [
66 { "choice": "net9.0" },
67 { "choice": "net10.0" }
68 ],
69 "replaces": "net9.0"
70 }
71 }
72}
73```
74
75Preserve from the source project:
76- SDK type, package references with metadata (PrivateAssets, IncludeAssets)
77- Properties (OutputType, TreatWarningsAsErrors)
78- Central Package Management and shared compile patterns
79
80### Step 2: Validate template.json
81
82Read and review the `template.json` for common authoring issues:
83
84Validation checks to perform:
85- **Required fields** — verify `identity`, `name`, and `shortName` are present
86- **Identity format** — use reverse-DNS format (e.g., `MyOrg.Templates.WebApi`)
87- **Parameter issues** — check datatypes are valid (`string`, `bool`, `choice`, `int`, `float`), choices have defaults, descriptions are present
88- **ShortName conflicts** — avoid names that collide with built-in CLI commands (`build`, `run`, `test`, `publish`). Check with `dotnet new list` to see if the name is already taken
89- **Post-action completeness** — verify post-actions have all required configuration
90- **Tags** — ensure language, type, and classification tags are set for discoverability
91
92### Step 3: Refine the template
93
94Based on validation results and user requirements:
95
961. **Add parameters** with appropriate types (string, bool, choice), defaults, and descriptions
972. **Add conditional content** using `#if` preprocessor directives for optional features
983. **Configure post-actions** for solution add, restore, or custom scripts
994. **Set constraints** to restrict which SDKs or workloads the template supports
1005. **Add classifications** and tags for discoverability
101
102### Step 4: Test the template locally
103
1041. Install the template from the local directory:
105 ```bash
106 dotnet new install ./path/to/template/root
107 ```
1082. Run a dry-run to verify the output:
109 ```bash
110 dotnet new mylib --name TestProject --dry-run
111 ```
1123. Create a test project and verify it builds:
113 ```bash
114 dotnet new mylib --name TestProject --output ./test-output
115 dotnet build ./test-output/TestProject
116 ```
1174. Verify all parameters produce the expected output
118
119### Step 5: Package for distribution
120
1211. Create a `.nuspec` or use `<PackAsTool>` in a packaging `.csproj`
1222. Include the template directory with `.template.config/template.json`
1233. Run `dotnet pack` to create the `.nupkg`
1244. Test installation from the `.nupkg`:
125 ```bash
126 dotnet new install ./path/to/package.nupkg
127 ```
128
129## Validation
130
131- [ ] `template.json` passes manual validation with zero errors
132- [ ] Template identity and shortName are unique and meaningful
133- [ ] All parameters have descriptions and appropriate defaults
134- [ ] Template can be installed, dry-run, and instantiated successfully
135- [ ] Created projects build cleanly with `dotnet build`
136- [ ] Conditional content produces correct output for all parameter combinations
137
138## Common Pitfalls
139
140| Pitfall | Solution |
141|---------|----------|
142| Identity format issues | Use reverse-DNS format (e.g., `MyOrg.Templates.WebApi`). Avoid spaces or special characters. |
143| ShortName conflicts with CLI commands | Avoid names like `build`, `run`, `test`, `publish`. Check by running `dotnet new list` to see if the name is already taken. |
144| Missing parameter descriptions | Every parameter should have a `description` and `displayName` for discoverability. |
145| Not testing all parameter combinations | Use `dotnet new <template> --dry-run` with different parameter values to verify conditional content works correctly. |
146| Hardcoded versions in template | Use `sourceName` replacement for project names and consider parameterizing framework versions. |
147| Not setting classifications | Add appropriate `classifications` (e.g., `["Web", "API"]`) for template discovery. |
148
149## More Info
150
151- [Custom templates for dotnet new](https://learn.microsoft.com/dotnet/core/tools/custom-templates) — official authoring guide
152- [template.json reference](https://github.com/dotnet/templating/wiki/Reference-for-template.json) — full schema reference
153- [Template Engine Wiki](https://github.com/dotnet/templating/wiki) — template engine internals