Create and publish your custom .NET project templates
In this blog I composed a set of useful tips, samples and materials on how to create, test, use and publish (Nuget.org) your own custom .NET project templates. If you are looking for a recent example, feel free to check out one of my own templates on Github. You can find more details about the template in this dedicated blog entry.
Work with templates
.NET Cli
- dotnet new: creates a new project, configuration file, or solution based on the specified template.
- dotnet new search: searches for the templates supported by
dotnet new
on NuGet.org. - dotnet new list: lists available templates to be run using
dotnet new
. - .NET default templates for dotnet new.
- Available templates for dotnet new.
Visual Studio
Default templates
- Common project (classlib, console) and item templates.
- ASP.NET and Blazor templates.
- WPF templates.
- Other.
Custom templates
Articles and documentation
- .Net templating Wiki on GitHub.
- How to create your own templates for dotnet new by Sayed Ibrahim Hashimi.
- Custom templates for dotnet new on Microsoft Learn.
- Create custom project and item templates on Microsoft Learn.
- Create project templates on Microsoft Learn.
- Tutorial: Create a template package on Microsoft Learn.
Videos
- How to create your own project templates in .NET by Nick Chapsas.
- Microsoft series with Sayed Ibrahim Hashimi:
- Creating your own .NET Project Templates.
- Create .NET Core Projects with the Command Line.
- Use an Existing .NET Core Project Template.
- Create a .NET Core Project Template.
- Create a .NET Core Project Template for Visual Studio.
- Add a Parameter to a .NET Core Project Template.
- Troubleshooting .NET Core Project Templates.
Code examples
- Template samples by Sayed Ibrahim Hashimi.
- .NET Boxed templates.
- .NET Template Samples.
- Refer to the Default templates section for official examples.
- Find community templates: Go to Nuget.org => Search (set Package type to ‘Template’).
Implementation
- Write code, implement template project.
- Define template configuration file (
template.json
). Find documentation here. - Define additional files to override
template.json
or define additional presentation configurations for .NET CLI (dotnetcli.host.json
) and Visual Studio (ide.host.json
) hosts. - Use
template.json
parameters in classes. - Use
template.json
parameters in.csproj
files.
Build package
- dotnet pack: packs the code into a NuGet package.
- pack command (NuGet CLI): creates a NuGet package based on the specified
.nuspec
or.csproj
file. - Adding nuget pack as a msbuild target.
- NuGet pack and restore as MSBuild targets.
- Create a NuGet package with the dotnet CLI.
- Create a NuGet package using MSBuild.
.nuspec
vs.csproj
for making nuget packages.
Find an example here: MightyMentorConsole.Template.csproj
.
Install and test locally
- Manage .NET project and item templates.
- How to test template changes locally by Sayed Ibrahim Hashimi.
- Install a template package from a file system directory.
Publish
This post is licensed under CC BY 4.0 by the author.