Custom "dotnet new" Templates for Your Team



There are a bunch of dotnet templates readily available for use. To list out all the templates, open up the command prompt and type in the following command,

dotnet new --list

That's all fine and dandy! But what about creating your templates? Say, for example, you are bootstrapping a multi-project (e.g. Web, Domain, Infrastructure) microservice-based solution and find it super helpful for others to use as well. You can easily ship your template to NuGet or other sources following some easy steps.

Recently, I'm working mostly with Blazor and Fluxor (A state management library just like Redux/Flow). I was doing some tedious work every time I wanted to start from scratch,

  • I've to use dotnet new blazorserver or dotnet new blazorwasm to create a new project.
dotnet new blazorserver -o BlazorServer
dotnet new blazorwasm -o BlazorWasm
Blazor Server
Fluxor.Blazor.Web 4.1.0
A zero boilerplate Redux/Flux framework for Blazor
services.AddFluxor(opt =>
{
    opt.ScanAssemblies(typeof(Program).Assembly);
    opt.UseRouting();
    opt.UseReduxDevTools();
});
Startup.cs / Program.cs
  • Add a Store folder and add necessary code files for Actions, Reducers, State, Feature, Effects, so on and so forth
  • Initialize the Store in the App.razor
<Fluxor.Blazor.Web.StoreInitializer />
App.razor
  • Import the <script src="_content/Fluxor.Blazor.Web/scripts/index.js"></script> in _Host.razor or index.html
<script src="_content/Fluxor.Blazor.Web/scripts/index.js"></script>
_Host.razor / index.html

I figured out that it would be easier if I just create some custom templates where all of this boilerplate stuff is already in place for me. Speaking of creating custom templates for Blazor + Fluxor, here's a shameless plug

GeekHour.AspNetCore.BlazorFluxor.Templates 1.0.1
Project templates Blazor and Fluxor.

Project Structure

The project structure I'm going for is following

BlazorFluxorTemplates
│   GeekHour.AspNetCore.BlazorFluxor.Templates.nuspec
│
└───Content
    ├───BlazorFluxorWasm
    │   │   project files
    │   │
    │   └───.template.config
    │           template.json
    │
    └───BlazorFluxorServer
        │   project files
        │
        └───.template.config
                template.json
Project Structure

There are a couple of ways you can use to pack up a template into a NuGet package,

The dotnet pack command and a .csproj file. Alternatively,  nuget pack command along with a .nuspec file

I'm going for the second route, hence I have a .nuspec file at the root of the project folder. To run nuget pack command, you would need the Nuget.exe. No worries! Just download it from, nuget.org/downloads.

So, I have two project folders under the Content folder, each one containing a different flavor of Blazor wired up with Fluxor. You can get a better look at those here,

GeekyHours/BlazorFluxorTemplates
Contribute to GeekyHours/BlazorFluxorTemplates development by creating an account on GitHub.

.NET template engine expects that you have a .template.config folder on the root of your runnable project. To turn that runnable project into a template with some desired configurations, you better have a template.json file under the .template.config folder.

{
    "$schema": "http://json.schemastore.org/template",
    "author": "Geek Hour",
    "classifications": [ "Web", "Blazor", "Fluxor" ],
    "identity": "GeekHour.Web.BlazorFluxorServer.CSharp.6.0",
    "name": "Blazor Fluxor Server Application",
    "shortName": "blazorfluxorserver",
    "tags": {
        "language": "C#",
        "type": "project"
    },
    "sourceName": "BlazorFluxorServer",
    "preferNameDirectory": true,
    "defaultName": "BlazorFluxorServer",
    "description": "Blazor server template wired with Fluxor"
  }
template.json

Pretty much everything is self-explanatory in the configuration file. To know more about what each of these flags does, refer to this official doc.

At this point, you are ready to install the templates locally using the following command,

> dotnet new --install .\BlazorFluxorTemplates\Content\

Template Name                     Short Name          Language  Tags
--------------------------------  ------------------  --------  -----------------------------
Blazor Fluxor Server Application  blazorfluxorserver  [C#]      Web/Blazor/Fluxor
Blazor Fluxor Wasm Application    blazorfluxorwasm    [C#]      Web/Blazor/Fluxor/WebAssembly

Time for creating the .nuspec file,

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>GeekHour.AspNetCore.BlazorFluxor.Templates</id>
    <version>1.0.1</version>
    <description>
      Project templates Blazor and Fluxor.
    </description>
    <authors>Geek Hour</authors>
    <license type="expression">MIT</license>
    <projectUrl>https://github.com/GeekyHours/BlazorFluxorTemplates</projectUrl>
    <repository type="git" url="https://github.com/GeekyHours/BlazorFluxorTemplates" branch="main"/>
    <tags>Blazor Fluxor Server WebAssembly Template</tags>
    <packageTypes>
      <packageType name="Template" />
    </packageTypes>
  </metadata>
  <files>
    <file src="Content\**\*.*" exclude="Content\**\bin\**\*.*;Content\**\obj\**\*.*" target="Content" />
  </files>  
</package>
GeekHour.AspNetCore.BlazorFluxor.Templates.nuspec

The important thing to notice here is the <files> node; You have to include the location of the template files here.

All done! Time to run the nuget pack command to create a Nuget package (.nupkg). I have downloaded Nuget.exe in C:\ drive, so first I've to go inside the directory and then specify the location of the .nuspec file following the pack command.

Now that you have the Nuget package (.nupkg), you can upload it the nuget.org so that the rest of the world can use your template.

Custom templates for dotnet new - .NET CLI
Learn about custom templates for any type of .NET project or files.
dotnet/templating
This repo contains the Template Engine which is used by dotnet new - dotnet/templating
sayedihashimi/template-sample
Contribute to sayedihashimi/template-sample development by creating an account on GitHub.