Once you have built your ASP.NET Core application, the next step is to run it. The .NET Command Line Interface (CLI) provides a simple command to execute your application. This guide will explain how to run an ASP.NET Core application using the CLI, including the necessary commands and explanations.

What is dotnet run?

The dotnet run command is used to run an ASP.NET Core application directly from the command line. This command builds the application (if it hasn't been built yet) and then starts it, allowing you to test and interact with your application locally.

When to Use dotnet run

You should use the dotnet run command when:

  • You want to quickly test your application after making changes.
  • You are developing your application and need to see the results in real-time.
  • You want to run the application without publishing it first.

How to Use dotnet run

To run your ASP.NET Core application, follow these steps:

Step 1: Open Your Command Line Interface

Open your terminal (macOS/Linux) or command prompt (Windows) and navigate to the directory containing your ASP.NET Core project.

Step 2: Run the Application

Use the following command to run your application:

dotnet run

This command will build the project (if necessary) and start the application.

Example

Suppose you have an ASP.NET Core project named MyWebApp. To run the project, you would do the following:

cd MyWebApp
dotnet run

After executing this command, you should see output indicating that the application is running. By default, it will be accessible at http://localhost:5000 or https://localhost:5001.

Specifying the Project File

If you have multiple projects in a solution and want to run a specific project, you can specify the project file:

dotnet run --project MyProject.csproj

Replace MyProject.csproj with the name of the project file you want to run.

Running with a Specific Configuration

By default, the dotnet run command runs the application in the Debug configuration. If you want to run the application in the Release configuration, you can specify it using the -c option:

dotnet run -c Release

This command runs the application with optimizations suitable for production deployment.

Conclusion

The dotnet run command is a powerful tool for executing ASP.NET Core applications directly from the CLI. By following the steps outlined above, you can easily run your application, test changes, and interact with it locally. Understanding how to effectively use this command is essential for any ASP.NET Core developer looking to streamline their development workflow.