Overview of .NET templates

Overview of .NET templates

This post gives an overview of .NET templates, which are starter structures for solutions, projects, or individual files.

As an example, the default .NET template for a new solution includes only the solution file. It can be customised to include additional items, such as a .gitignore file and a README.md file, bundled as solution items. This template can be found here.

Overview

Here are a few resources to help familiarise oneself with .NET templates.

Nick Chapsas' YouTube Video offers a brief introduction to what is possible with .NET templates.

Microsoft’s Tutorial provides a practical walk-through for creating individual file templates, project templates, and organising multiple templates into a package.

For personal use, a single, regularly updated template package is sufficient. Beyond personal use, templates are grouped by purpose/technology. For example, Duende Identity Server templates provide a starting point for projects using Identity Server, while Avalonia UI templates help with creating AvaloniaUI applications.

.NET default templates is the source code for the default templates included with .NET. These can be referenced when customising or extending existing templates. In particular, here is the empty solution template.

Template Management

Here are a few useful .NET CLI commands for managing .NET templates.

Display all available templates: dotnet new list

Display all installed template packages: dotnet new uninstall

The output looks like this:

TryAndTrash.Toolkit.Templates
  Version: 1.0.0
  Details:
	 Author: Try and Trash
	 Reserved: ✘
  Templates:
	 Try and Trash Async Project (tat.consoleasync) C#
	 Try and Trash String Extensions (tat.stringext) C#
	 Try and Trash New Solution (tat.solution) C#
  Uninstall Command:
	 dotnet new uninstall TryAndTrash.Toolkit.Templates

The following commands are executed at the root level of a template package repository, with try-and-trash-templates used as an example.

Install:

  • dotnet pack to package the templates into a NuGet package.
  • dotnet new install ./bin/Release/TryAndTrash.Toolkit.Templates.1.0.0.nupkg to install the package.

Uninstall: dotnet new uninstall TryAndTrash.Toolkit.Templates (this is the same command that dotnet new uninstall suggests).

Update: make changes, uninstall the existing package, pack and reinstall the updated version.

Next Steps

.NET templates enable a developer to accumulate a personal toolkit to better tackle recurring tasks:

  • File-level templates for reusable configuration files and code snippets.
  • Project-level templates for a customised version of every project type.
  • Solution-level templates for a collection of starting files or multi-project approaches.

Read more