ASP.NET Core – How to work with Command Line

How we can make use of the dotnetcore CLI for building, testing and deploying our applications without manual interventions - such as automation.

One of the main aspects of the dotnet core SDK is the dotnet core CLI. It is a set of tools and commands which make development and deployment easy with dotnet core without actually depending on the Visual Studio. Using the CLI we can create a new project, clean, build and publish code along with support for running unit tests. The CLI also comes with readymade templates with boilerplate code for faster bootstrapping.

Create New Project –

Let’s assume we need to create a new web api project in dotnet core. Using CLI we can create using the below command:

> dotnet new webapi --name MyWebApiProject

In the above command new command accepts the type of project we are interested in to create. The options can be console, mvc, webapi, xunit, mstest, nunit, angular, react, redux for example. The –name passes the name of the project to be created.

In our above command, we get a new webapi project created with all the boilerplate code added. Also the necessary dependencies and libraries are preinstalled.

We can then use any standard code editor (Visual Studio Code most recommended for dotnet core) for taking the development further.

Clean, Build and Run Project –

Once the development is complete, the next steps would be to run the application. For that, we can use the below commands in CLI under the project path:

> dotnet clean && dotnet build && dotnet run

the above command is actually three commands piped using an && operator. The above command cleans, build and runs the project. These commands implicitly restore the packages if necessary and generate the executables. For explicitly restoring packages. We can use the below command under the project path:

> dotnet restore

The above command connects to the default nuget source for dependencies and adds the missing librares into the project.

Publish Code for Deployment –

Once the build is ready and when needs to be deployed, we would require to publish our code for release ready executables. The below command does the operation:

> dotnet publish

Which builds the code and generates the executables under the default release path bin\Release\netcoreapp\publish

The release path can also be specified during publish as below:

> dotnet publish -output <output path>

For publishing using a publish profile (pubxml files typically generated in Visual Studio publish, which reside under /Properties/PublishProfiles/ path in the project)

> dotnet publish <project csproj> /p:PublishProfile=<FolderProfileName>

A pubxml file can be as below:


<Project>
  <PropertyGroup>
    <PublishProtocol>Kudu</PublishProtocol>
    <PublishSiteName>nodewebapp</PublishSiteName>
    <UserName>username</UserName>
    <Password>password</Password>
  </PropertyGroup>
</Project>

Buy Me A Coffee

Found this article helpful? Please consider supporting!


Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *