Distribute Visual Studio project templates

Pieterjan De Clippel
4 min readJul 7, 2022

--

Documentation

Setup the solution

  • In Visual Studio, create a new project → Class Library
  • We can start by editing our csproj file. The project file is only there to allow us to bundle the files as Content build action into a nuget package/vsix extension

The most important fields are explained here

  • PackageType: set to Template
  • IncludeContentInPack: Includes project files with Build action = Content in the NuGet package
  • ExcludeBuildOutput: Should ensure that the contents of the bin and obj folders aren’t packed. The contents of the node_modules folder on the other hand will be packed if we don’t exclude them explicitly
  • NoDefaultExcludes: is set to true, in order to have the leading-dot files included in the template. This is also the case in the spa-templates
  • All other fields are classic NuGet package properties

Furthermore, for the release build, we’re including the files as Content. I’ve also added the Debug configuration with the *.cs files as Compile, in order to have a little bit more IntelliSense while writing the template.

Adding the template

  • You can now setup the following project structure:
  • Identity: Just some unique name to identify your template
  • GroupIdentity: Templates with the same GroupIdentity are shown as a single item on the new-project dialog. On the next dialog the user will have to choose from templates within that Group
  • ShortName: used for the dotnet new [shortName]command
  • SourceName: This will be replaced anywhere in your files within your template with the project name entered by the user
  • DefaultName: self-explanatory
  • Tags: MUST be defined if you want to use the template in Visual Studio. Type = item, project or solution — Language = C#
  • Symbols: fields that will (or will not) appear on the new-project window

Adding a GitHub workflow

Just for the show we could add a github workflow now.

/.github/workflows/publish-master.yml

Publishing this git repo to github.com would already publish your template package to nuget.org and make it installable.

But because this is a demo, we’ll be installing the package directly now. Open a cmd in the root directory of the solution and run:

dotnet build --configuration Release
dotnet pack --no-build --configuration Release
cd MyCompany.AspNetCore.ServerSideRendering.Templates\bin\Release
dotnet new --install MyCompany.AspNetCore.ServerSideRendering.Templates.6.0.0.nupkg

Et voila — the template is installed. Now you can go to your repos folder and generate a project

mkdir C:\repos\ssr-demo
cd C:\repos\ssr-demo
dotnet new web-ssr

Using the template in Visual Studio

At the moment, the template is available through the dotnet cli, and also from Visual Studio. However, it looks a little too flat at the moment:

  • We can add a ide.host.json to the .template.config directory like this:
/.template.config/ide.host.json
  • And the angular logo in the same folder (as angular.png).
  • Now you have to update the PackageVersion in the csproj to 6.0.1 and publish the package again:
dotnet build --configuration Release
dotnet pack --no-build --configuration Release
cd MyCompany.AspNetCore.ServerSideRendering.Templates\bin\Release
dotnet new --install MyCompany.AspNetCore.ServerSideRendering.Templates.6.0.1.nupkg

When you create a new project, you’ll see the following:

Update template packages

After publishing a new version to nuget.org, users will have to update the template package like this:

dotnet new --update-apply

To be continued, some time in the future…

External resources

Conversion from .csproj.in to csproj

Happens here, before the template is packed.

--

--