8 January 2017
As you continue your journey with .NET Core, one of the most important tools you’ll use is the .NET CLI (Command Line Interface). The CLI allows you to perform many tasks related to building, running, and testing your .NET Core applications directly from the terminal. It’s an essential part of the developer workflow, especially when you’re working with cross-platform tools like macOS.
The best part? The .NET CLI is simple to use and intuitive, making it a great option for both beginners and experienced developers. In this post, we’ll take a look at some of the most important commands that will help you as you develop .NET Core applications.
1. What is the .NET CLI?
The .NET CLI is a command-line tool that provides an interface for creating, building, and managing .NET projects. It is included when you install the .NET SDK (which we did earlier in the setup process) and can be accessed through your terminal or command prompt.
With the CLI, you can perform operations like creating a new project, compiling your code, running tests, and more—all without needing to use a visual IDE like Visual Studio.
2. Important .NET CLI Commands
Let’s go over the most commonly used .NET CLI commands:
a. dotnet new
The dotnet new
command is used to create a new .NET Core project. It’s the starting point for any new .NET Core application. For example:
dotnet new console -n MyApp
This command creates a new console application called MyApp. You can also use this command to create different types of projects, such as:
dotnet new web
: Creates a new ASP.NET Core web application.dotnet new mvc
: Creates a new MVC web application.dotnet new classlib
: Creates a new class library.
The dotnet new
command is the fastest way to scaffold a new project and get started with writing your code.
b. dotnet build
After creating your project and writing some code, you’ll need to build the application. Building compiles the code into an executable, making it ready for running or deployment. To build your project, use the following command:
dotnet build
When you run this command, the .NET CLI will look for the project file (e.g., MyApp.csproj
) in your current directory, compile the code, and place the output in the bin
directory.
c. dotnet run
Once your project is built, you’ll want to run the application to see it in action. The dotnet run
command both builds and runs your application in one step. This is a convenient shortcut that eliminates the need to run dotnet build
first. Simply run:
dotnet run
You should see your application’s output, like the familiar “Hello World!” if you’re working on a console app.
d. dotnet test
Testing your application is an important part of the development process. With the dotnet test
command, you can run unit tests for your .NET Core application. It will search for any test projects in the directory, build them, and execute them.
To run the tests, you can use:
dotnet test
If you have tests defined in your project, you’ll see the results of each test in the terminal. If you don’t have tests yet, this command will inform you that no tests were found.
3. How to Use the .NET CLI for Running and Testing Applications
Let’s put these commands into practice. Let’s assume you’ve already created a simple console application (like the “Hello World” example we made earlier). Here’s how you can use the CLI to run and test your application:
- Create the Application: If you haven’t already, create a new console app:
dotnet new console -n HelloWorldApp cd HelloWorldApp
- Build the Application: Before running the app, build it to ensure everything is compiled:
dotnet build
This will compile the app, generating any necessary files and placing them in thebin
folder. - Run the Application: Now, run the application with:
dotnet run
You should see the output printed to the terminal, like:Hello World!
- Test the Application: For now, if you don’t have any tests written yet, the
dotnet test
command will inform you that no tests are present. But as you develop your app, you can write unit tests and run them via:dotnet test
This command will help you ensure that your code works as expected.
4. Other Useful .NET CLI Commands
Here are a few more helpful commands you might use during your development process:
dotnet restore
: Restores the dependencies and project tools that are required for the project. You’ll often run this after adding new NuGet packages.dotnet restore
dotnet publish
: Prepares your app for deployment by compiling and packaging it into a format suitable for distribution.dotnet publish
dotnet help
: Displays a list of available commands and options. If you’re ever unsure about a command, you can usedotnet help
to get more information.dotnet help
5. Why Use the .NET CLI?
The .NET CLI is an essential tool for anyone working with .NET Core. It offers several advantages:
- Cross-platform: Since it works on macOS, Linux, and Windows, you can use it regardless of your operating system.
- Quick and efficient: Running simple commands like
dotnet new
,dotnet build
, anddotnet run
allows you to quickly iterate on your application and test your code without opening a full-fledged IDE. - Consistency: The same commands work across different development environments, so you don’t have to worry about discrepancies between different tools or systems.
6. Conclusion
The .NET CLI is an indispensable tool in the .NET Core ecosystem. It enables developers to streamline the process of building, running, and testing applications directly from the terminal. Now that you’re familiar with the basics, you can use these commands to develop your .NET Core applications more efficiently.