The whole process of .NET Core custom project template



Introduced earlierCustom project templatesA simple way is introduced in – by creating a project and exporting it as a project template. This time we will use dotnet cil (hand scaffolding) to create the project template.

So, let's first look at the project templates currently supported by dotnet:

It can be seen that many template projects have been provided in dotnet, so how to create your own project through dotnet cli based on the accumulated content of project development to improve development efficiency?

1. Implement custom project templates

A custom template project template definitely requires template implementation. This time, we will use the project structure in the previous article as a template project to implement a custom project template.


Next follow the steps to create the template project

Create a template configuration file

Create a folder in the project root folder: .template.config and create template.json under the directory. As shown below:

Define project template related information

Modify the content of template.json to the following:

    "author": "cwsheng", //must
    "classifications": [ "Web/WebAPI" ], //Required, the Tags of this corresponding template
    "name": "ApiTmp", //Required, Templates of this corresponding template
    "identity": "ApiTemplate", //optional, the unique name of the template
    "shortName": "apitmp", //Required, the Short Name of the corresponding template
    "tags": {
      "language": "C#" ,
    "sourceName": "ApiTemplate", // optional, the name to replace
    "preferNameDirectory": true // optional, add a directory  

Note: shortName and sourceName

  • author: Template author
  • classifications: Template feature identifiers. The configuration in the example above is because my custom template includes console and webapi.
  • identity: a unique name for this template
  • name: the template name that the user sees
  • shortName: Short name. Using short names will facilitate usage when creating template projects using CLI commands.
  • sourceName: The key text for template replacement, you need to pay attention when using it, you must select the appropriate replacement text, otherwise it is easy to accidentally damage the code.

Project Template Installation

Next, go to the project directory and install this template into the cli. usedotnet new -iInstall the template.

dotnew new -i TempDemo

Uninstall the project template:

dotnet new -u template directory

Create the project with the command:

dotnet new apiTmp -n Test -o .

apiTmp: is the shortName specified in the template configuration file.

-n: specifies that all text appearing in the item is to beMyJobreplace withTest, because the sourceName in the configuration is ApiTemplate, which can be configured as needed.

-o: Specifies the output directory of the generated project. The above command generates the project output to the current directory.

The result is as follows:


This creation of project templates through dotnet cli has been completed, but this alone is not enough for daily work needs.

like:Project A needs logging, but project B does not need logging; project A needs to develop a PG database as the storage method, and project B needs to develop MySQL as the storage method.

So can these questions meet the project requirements according to the settings at the time of project creation?

2. Advanced usage of template project

File filtering: formulate file and code content according to parameter filtering

a) Define parameters: add the following in template.json: EnableAuditLog-parameter name

  "symbols": {
    // Whether to use the audit log function
    "EnableAuditLog": {
      "type": "parameter", //it is a parameter
      "dataType": "bool", // parameter of type bool
      "defaultValue": "false" //The default is not enabled
  "sources": [
      "modifiers": [
          "condition": "(!EnableAuditLog)", //Condition, determined by the EnableAuditLog parameter
          "exclude": [ //Exclude the following files

b) Cancel the effective location of the audit log:

c) Regenerate the template to view the parameters supported by the current template:

d) Create a project to check whether the settings take effect:

Select Execute: Select assembly reference based on variable value

a) Add parameter: DbType specifies the option database type

"symbols": {
    //Data source type "DbType": {
      "type": "parameter", //it is a parameter
      "datatype": "choice", //option type
      "choices": [ //option value
          "choice": "MsSQL",
          "description": "MS SQL Server"
          "choice": "MySQL",
          "description": "MySQL"
          "choice": "PgSQL",
          "description": "PostgreSQL"
          "choice": "SQLite",
          "description": "SQLite"
          "choice": "None",
          "description": "None"
      "defaultValue": "None",
      "description": "The type of SQL to use"
    "MsSQL": {
      "type": "computed",
      "value": "(DbType == \"MsSQL\")"
    "MySQL": {
      "type": "computed",
      "value": "(DbType == \"MySQL\")"
    "PgSQL": {
      "type": "computed",
      "value": "(DbType == \"PgSQL\")"
    "SQLite": {
      "type": "computed",
      "value": "(DbType == \"SQLite\")"
    "None": {
      "type": "computed",
      "value": "(DbType == \"None\")"

b) Modify the project file: select the corresponding assembly according to the conditions

<Project Sdk="Microsoft.NET.Sdk">


  <ItemGroup Condition="'$(None)'!='True'">
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.8" />
  <ItemGroup  Condition="'$(SQLite)' == 'True'">
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.8" />
  <ItemGroup  Condition="'$(MsSQL)' == 'True'">
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.8" />
  <ItemGroup  Condition="'$(PgSQL)' == 'True'">
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />

c) Using the database, synchronous processing

#if (!None)
            //Audit log storage
            services.AddDbContext<AuditLogDBContent>(options =>
                string conn = Configuration.GetConnectionString("LogDB");
#if Sqlite
                options.UseSqlite(conn, options =>
#if PgSQL

#if MsSQL


d) After updating the template, check that the corresponding parameters have been generated

e) Create a project to check the effective status:

// do not use the database
dotnet new apiTmp -n AA -D None

//Use the PG database
dotnet new apiTmp -n BB -D PgSQL

You can see that the project reference and program code are generated according to the incoming parameters.

3. Project template package and release

Of course, good things can't be used only for yourself, but how should the generated templates be used by everyone? – Nuget package release

a) Create a nuspec file in the template root directory: apiTmp.nuspec

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="">
    <description> WebApi Template </description>
      <packageType name="Template" />

b) Use Nuget Pack command to pack

 nuget pack apiTmp.nuspec -OutputDirectory .

c) Publish to nuget service, download and use for friends


Template projects created through dotnet cli can more easily generate corresponding project structures according to project requirements, improving development efficiency.

You can pack the accumulated good projects into a template project for your friends to use


So far, this article about .NET Core custom project templates is introduced. For more related .NET Core custom project templates, please search for previous articles on developpaer or continue to browse the related articles below. I hope you will support developpaer more in the future. !

Recommended Today

How to create JavaScript?

FusionCharts Suite XT is a comprehensive cross-platform, cross-browser JavaScript charting suite including FusionCharts XT, PowerCharts XT, FusionWidgets XT, FusionMaps XT. Supports ASP, ASP.NET, PHP, JSP, ColdFusion, Ruby on Rails, JavaScript, and even simple HTML pages. It's your trusted JavaScript charting solution, and 450,000 users worldwide currently choose Fusioncharts to make professional JavaScript charts. Use a […]