Create dotnet new template with multiple projects

pack and install your dotnet template

StoyanShopov
2 min readNov 12, 2020

Motivation

If you want to start a project with ASP.NET Core, probably you will spend around 4–6 hours to set up your project.

That’s why there are so many ready to use templates, which use different front-end frameworks, databases, etc.

My favorite one is:

https://github.com/NikolayIT/ASP.NET-Core-Template

We wanted to create a dotnet template for it. To be as simple as possible.

Problem

There are many articles and Microsoft documentation on how to create a dotnet template project, but just a bit on how to create a template with multiple projects and files.

Every time the dotnet pack command, generating nuget files for each project, after that some files are missing, the dotnet pack command runs successfully, but the template is missing and many other issues.

So since I didn’t found a lot of information, I’ve started making heuristics to figure it out, how it’s done.

Solution

I will use, as an example the same project, but with a previous commit (without the template files)— https://github.com/NikolayIT/ASP.NET-Core-Template/tree/e3f2c4d156960858ec98f0ae9e641868d55d3df1

You can clone the project and try the same things.

In the folder, where is placed your “.sln” file, create a folder called “.template_config” and inside that folder, create a file —”template.json

  1. Example

2. Real example

After that, in the root folder, create a nuget.csproj file.

  1. Example

2. Real Example

Now it's time for some dotnet commands.

Go to your root folder, open the powershell console.

Create a nuget package

dotnet pack .\nuget.csproj

Install the nuget package

dotnet new -i .\YourNugetPackageName.nupkg

Now you will see a list with all of the available templates for dotnet new.

Your template should be there as well, so you can type:

dotnet new YourTemplateName

Now you can go and publish it on https://www.nuget.org/

Summary

You will need to create two folders, one for the template and one for the nuget package. You will need to describe some things, like a name, id, description, version, etc.

Pack, install, and run dotnet new.

Finally

If you found something useful in this article, you can leave a star in GitHub:

https://github.com/NikolayIT/ASP.NET-Core-Template

If you have any questions, please feel free to contact me:

dotnet new template multiple projects pack asp.net core template.json

--

--