Create a Custom Template for Dotnet New | by Donovan Brown | Jul, 2022

Programming Tips and Tricks

Stop wasting time cleaning up starter projects

Photo by Roman Synkevych 🇺🇦 on Unsplash

I often use the dotnet new webapi command when starting a new project. The first thing I must do is clean up the project. I delete the files I don’t need, and I rename and modify the files I want to keep. Then, I thought, “There must be a way to define my own template so I can skip the cleanup.” Well, there is.

In this article, I show you how to create a custom template to use with the dotnet new command. This way, you get the project you want without having to clean up the sample code.

Funny enough, the first thing I must do is create a Web API project and clean it up. In a folder named template, I run dotnet new webapi to create a sample project.

dotnet new webapi --no-https -n Cleanwebapi

Then, as always, I clean up the sample code. I will not need the weather forecast file, so I delete it.

ri .\Cleanwebapi\WeatherForecast.cs

I also rename the default controller file to CleanWebApiController.

ren .\Cleanwebapi\Controllers\WeatherForecastController.cs CleanwebapiController.cs

Now I open CleanwebapiController.cs and find and replace WeatherForecast to Cleanwebapi. I remove the Summaries array. Finally, I replace the GET method and add a POST method. In the GET method, I log that it was called and return “Hello World.” In the POST method, I log the body that was posted. The log messages will be visible when the code is run.

With the project updated, I execute it to make sure that it compiles and runs.

VS Code terminal showing project running
The template is working

Next, I create an HTTP file using the REST Client extension for Visual Studio Code to send requests to test the methods. I test the GET method first with a request to localhost on port 5287 to the cleanwebapi route.

Sending the request returns a status code of 200, the value “Hello World,” and the terminal shows the logged message. Now I test the Post method with a JSON body. Sending this request also returns a status code of 200, and the terminal shows the logged message of the JSON body.

VS Code terminal showing log messages
Application logs

With the code tested, I stop it and delete the bin and obj folders so they are not included in the template.

ri .\bin\ -fo -r
ri .\obj\ -fo -r

To turn these files into a template, I add a .template.config folder and place a template.json file inside. Note: The commands below (mcd and touch) require the Trackyon.Utils PowerShell module.

mcd .template.config
touch template.json

First, I add a schema to the file which will enable Visual Studio Code to offer IntelliSense. While editing with the schema in place, pressing CTRL + space lists all the properties that can be added to this file.

VS Code editor showing all possible elements for the template.json file
All possible elements

I begin with author and enter my Twitter handle. Then I add classifications, which are characteristics that a user can use to search templates. The classifications also appear in the tags column when the available templates are listed using the dotnet new command. Next, I add a unique name for the template in the identity property. The name property is the value the user sees when the available templates are listed.

The shortName is used from the command line and is what is typed after dotnet new. Setting the preferNameDirectory to true will create a directory instead of creating the content directly in the current folder. The sourceName will be replaced throughout the template with the name provided by the user. This will be replaced in files and file names. Take note of the casing of this value and make sure it matches, or it will not be replaced. Finally, I use the tags property to set the language and type.

With the template.json file complete, I can now install and use this template. To install, I use the dotnet new command with the -i option pointing to the current folder Cleanwebapi.

dotnet new -i .

The output of this command shows the new “Clean ASP.NET Core Web API” template. With the template installed, I create a new project using the cleanwebapi template.

dotnet new cleanwebapi -n Test

Listing the contents of the new test folder shows the Test.csproj file was created using the name passed in.

Directory: F:\temp\template\TestMode Length Name
---- ------ ----
d---- Controllers
d---- Properties
-a--- 127 appsettings.Development.json
-a--- 151 appsettings.json
-a--- 527 Program.cs
-a--- 327 Test.csproj

Listing the controller directory shows the controller file name as TestController.cs.

Directory: F:\temp\template\Test\ControllersMode Length Name
---- ------ ----
-a--- 600 TestController.cs

Lastly, opening that file shows the namespace and class name were also created using the name provided with the -n option.

Creating a custom template is easy and can save you time when using dotnet new.

Thanks for reading. Until next time!

News Credit

%d bloggers like this: