How to Run a .NET Application Using the dotnet Command
If you are new to .NET, one of the first commands you will use after creating a project is dotnet run.
The dotnet run command is used to build and run a .NET application from its source code. It is commonly used during development because it lets you quickly compile your application and start it without manually running the compiled executable.
In this tutorial, you'll learn how to use dotnet run, how it works, and some useful options you can use when developing .NET applications.
What Is dotnet run?
dotnet run is a .NET CLI command that runs the application from its project source code.
The basic syntax is:
dotnet run
When you execute this command inside a .NET project directory, .NET will:
Locate the project file.
Restore dependencies if required.
Build the project.
Start the application.
For example:
dotnet run
You might see output similar to:
Building...
Hello, World!
The exact output depends on the application you are running.
Prerequisites
Before using dotnet run, make sure the .NET SDK is installed on your computer.
You can check the installed SDK version with:
dotnet --version
Example:
10.0.100
You can also see all installed SDKs with:
dotnet --list-sdks
If the dotnet command is not recognized, you may need to install the .NET SDK.
Step 1: Create a .NET Console Application
Let's create a simple console application.
Open a terminal or command prompt and run:
dotnet new console -n HelloDotNet
This creates a new project named HelloDotNet.
Move into the project directory:
cd HelloDotNet
Your project will contain files similar to:
HelloDotNet
│
├── HelloDotNet.csproj
├── Program.cs
└── obj
The .csproj file is the project file, while Program.cs contains the application code.
Step 2: Open Program.cs
The generated Program.cs file may contain:
Console.WriteLine("Hello, World!");
This program simply writes a message to the console.
Step 3: Run the Application
Make sure you are inside the project directory:
cd HelloDotNet
Then run:
dotnet run
You should see:
Hello, World!
That's it. You have successfully run a .NET application using the dotnet command.
How dotnet run Works
A common question is:
Does dotnet run build the application?
Yes.
During normal development, dotnet run can perform the necessary restore and build steps before launching the application.
Conceptually, you can think of it as:
Source Code
↓
Restore dependencies
↓
Build application
↓
Run application
This makes dotnet run particularly convenient during development.
Running an Existing .NET Project
If you have already downloaded or cloned a .NET project, navigate to the directory containing the project file.
For example:
cd MyApplication
Then run:
dotnet run
If the directory contains a project such as:
MyApplication.csproj
the .NET CLI can use that project automatically.
Running a Specific Project
Sometimes a solution contains multiple projects.
For example:
MySolution
│
├── MyApp.Api
│ └── MyApp.Api.csproj
│
├── MyApp.Core
│ └── MyApp.Core.csproj
│
└── MyApp.Tests
└── MyApp.Tests.csproj
In this situation, you can specify the project you want to run:
dotnet run --project MyApp.Api/MyApp.Api.csproj
You can also provide the full path if necessary:
dotnet run --project "C:\Projects\MySolution\MyApp.Api\MyApp.Api.csproj"
Running a .NET Application on a Specific Port
For ASP.NET Core applications, you may want to specify the URL and port.
You can use:
dotnet run --urls "http://localhost:5000"
Your application will then listen on:
http://localhost:5000
You can open that address in your browser.
For HTTPS, your application may also use a URL such as:
https://localhost:7001
The actual port depends on your application's configuration and launch settings.
Passing Arguments to Your Application
You can also pass command-line arguments to your .NET application.
For example:
dotnet run -- John
Your application can access the argument through the application's command-line arguments.
The -- separator tells the .NET CLI that the following arguments should be passed to the application rather than interpreted as dotnet run options.
For example:
dotnet run -- --environment Development
This distinction becomes particularly useful when your application itself accepts command-line parameters.
Using dotnet run With a Solution
You can also run a project from a solution directory.
However, if the solution contains multiple executable projects, it is usually clearer to specify the project explicitly:
dotnet run --project MyApp/MyApp.csproj
This avoids accidentally trying to run the wrong project.
Running a Specific .NET Configuration
.NET projects commonly use configurations such as:
Debug
Release
You can specify a configuration with:
dotnet run --configuration Release
The shorter form is:
dotnet run -c Release
For development, the default configuration is generally Debug.
Running Without Rebuilding
If you have already built your application and don't want dotnet run to build it again, you can use:
dotnet run --no-build
For example:
dotnet run --no-build
This can be useful when you know the application has already been built and you simply want to start it.
Running Without Restoring Packages
You can also prevent an implicit package restore:
dotnet run --no-restore
This can be useful in environments where dependencies have already been restored.
dotnet run vs dotnet build
These commands are related but serve different purposes.
For example:
dotnet build
only builds the project.
Whereas:
dotnet run
is intended to build and launch the application for development.
dotnet run vs Running the Compiled Application
There are different ways to execute a .NET application.
During development, you might use:
dotnet run
After publishing the application, you might execute the generated application using the appropriate deployment format.
For example, a framework-dependent application can commonly be started with:
dotnet MyApplication.dll
The important distinction is:
dotnet run
↓
Development workflow
↓
Build + launch project
while:
dotnet MyApplication.dll
↓
Run an already-built application
Running an ASP.NET Core Web API
The same command can be used with an ASP.NET Core Web API.
For example, create a Web API project:
dotnet new webapi -n MyApi
Move into the directory:
cd MyApi
Then run:
dotnet run
You will typically see output indicating that the application is listening on one or more URLs.
For example:
Now listening on: https://localhost:7001
Now listening on: http://localhost:5000
The actual ports may be different depending on the project configuration.
Running a .NET Application From Visual Studio
You don't have to use the terminal every time.
In Visual Studio, you can open the .sln or .csproj file and run the application using the Start or Run button.
However, knowing the dotnet run command is valuable because it works from:
Command Prompt
PowerShell
Windows Terminal
macOS Terminal
Linux terminal
CI/CD environments
It is also useful when working with Docker, GitHub Actions, Azure, and other development environments.
Common dotnet run Problems
1. 'dotnet' is not recognized
If you see an error such as:
'dotnet' is not recognized as an internal or external command
the .NET SDK may not be installed or the dotnet executable may not be available through your system's PATH.
Check:
dotnet --version
If that command fails, install the appropriate .NET SDK.
2. No Project Found
If you execute:
dotnet run
in a directory that does not contain a project, you may receive an error indicating that no project could be found.
Navigate to the directory containing the .csproj file:
cd MyApplication
Then run:
dotnet run
3. Build Errors
If your application contains compilation errors, dotnet run cannot successfully launch it.
For example:
Build failed.
In this situation, run:
dotnet build
to see the compilation errors.
Fix the errors and then run:
dotnet run
4. Port Already in Use
An ASP.NET Core application may fail to start if another application is already using the configured port.
For example:
Address already in use
You can run the application on another port:
dotnet run --urls "http://localhost:5050"
Useful dotnet run Commands
Here is a quick reference:
# Run the current project
dotnet run
# Run a specific project
dotnet run --project MyApp/MyApp.csproj
# Run using Release configuration
dotnet run --configuration Release
# Short form
dotnet run -c Release
# Run without building
dotnet run --no-build
# Run without restoring packages
dotnet run --no-restore
# Run ASP.NET Core on a specific URL
dotnet run --urls "http://localhost:5000"
# Pass arguments to the application
dotnet run -- argument1 argument2
Frequently Asked Questions
What does dotnet run do?
dotnet run builds and runs a .NET project, making it a convenient command for local development.
Do I need to run dotnet build before dotnet run?
No. You normally don't need to run dotnet build first. dotnet run can perform the required build step.
Can I use dotnet run for ASP.NET Core?
Yes. dotnet run can be used to launch ASP.NET Core applications such as Web APIs, MVC applications, and other web applications.
Can I specify which project to run?
Yes. Use:
dotnet run --project path/to/project.csproj
Can I specify the port?
Yes. For ASP.NET Core applications, you can use:
dotnet run --urls "http://localhost:5000"
Is dotnet run used in production?
It is primarily a development-oriented command. For production deployments, applications are commonly built and published using dotnet publish and then deployed using an appropriate hosting model.
Conclusion
The dotnet run command is one of the most useful commands in the .NET CLI.
For a typical development workflow, you can simply navigate to your project directory and run:
dotnet run
It is useful for console applications, ASP.NET Core applications, Web APIs, and many other .NET project types.
Once you become comfortable with dotnet run, you can combine it with other .NET CLI commands such as:
dotnet new
dotnet restore
dotnet build
dotnet test
dotnet publish
Understanding these commands will give you a solid foundation for working with the .NET CLI.
Looking for 1:1
Comments
Post a Comment