Creating DotNetCore WebAPI using CLI

Photo by Caspar Camille Rubin on Unsplash

Welcome, all. Before going into the blog, I have a statutory warning for you “This blog is only useful for very beginners and slow learners like me.” It is a good thing that Microsoft has released the dotnet core version for Linux and mac excluding, Windows. Considering working on a Linux machine, the obvious choice to use dotnetcore is the terminal/cli tool. Let us check how to create a new dotnetcore project using CLI commands by providing all the options we need in the project. The project we are going to see today is the web API project.

Project type: webapi

Creating a new boilerplate without any preferences

The command reference for webapi is available in Microsoft docs.

The command to create an empty webapi project with any preferences in the boilerplate is,

dotnet new webapi — name ProductApi

dotnet new webapi -n ProductApi

Both of them are the same. You can use any one of those.

If you are not providing the project name ( — name ProductApi) in the above commands, the command will automatically take the folder name as the project name. If you are in the folder “ProjectApi”, that will be considered as the project name. If you are not providing it in the command.

Switch off HTTPS

If you want to switch off HTTPS in your project at the time of creation; then, you need to pass the following parameter in the command.

— no-https

And this flag or parameter will not work if you are using authentication methods like singleorg, mutliorg, individualb2c, individual in the project.

Choose framework

The next big thing we would be looking forward to is choosing the framework. As a developer, you should choose the right framework for your project right?

To give a preference we need to know what are framework versions we have installed in our system. We have a lot of ways to identify that. But for me the most simple way to check that is,

dotnet new webapi -h

while executing the above command you will see the help text for the new command but in the section -f| — framework, it will list the framework versions you have installed. Check that and identify what framework you need to configure in your new project and then pass the parameter as follows. Consider you need to configure dotnet version 5.0, then the parameter you need to pass is,

dotnet new webapi — framework net5.0

dotnet new webapi -f net5.0

The default values of framework targets in dotnet core are as follows,

SDK 6.0 -> net6.0

SDK 5.0 -> net5.0

SDK 3.1 -> netcoreapp3.1

SDK 3.0 -> netcoreapp3.0

SDK 2.1 -> netcoreapp2.1

Both commands are the same, you can use either of them.

To disable swagger in your project

Swagger is yet another useful option to check the responses while developing. If you think you can use postman for development instead of swagger, you can disable it while creating the project. To do that pass the following parameter to disable it.

dotnet new webapi — no-openapi true

the above command will instruct the dotnet tool not to include swagger while creating the project.

Other Params

There are few other params available with the command and most of them fall under the authentication category. We can see those in detail in another article.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store