.NET Core  

Run and Debug .NET Core in VS Code

Introduction

Running and debugging a .NET Core Web API project in Visual Studio Code is straightforward when the development environment is properly configured. With the required SDK and extensions installed, VS Code can now easily build, run, and attach a debugger to your application.

This article provides a step-by-step guide to help you run and debug a .NET Core Web API project efficiently using VS Code.

Prerequisites

Before starting, ensure that your system has the following tools installed:

  • Visual Studio Code

  • .NET 8 SDK

To confirm that the .NET SDK is installed correctly, run the following command in your terminal:

dotnet --version

The command should return a version number starting with 8.

Opening the Project in VS Code

Begin by navigating to your project directory, open CMD there, and run the below command.

code .

Restoring and Building the Project

Before running the application, it is important to restore dependencies and build the project. This ensures that all required packages are available and that the code compiles successfully.

Restore/Build dependencies:

dotnet restore
dotnet build

If there are any issues during this step, they should be resolved before proceeding to debugging.

Starting the Debugging Process

Step 1: Initiate Debugging

Use the Run menu in Visual Studio Code to start the debugging process. You can either select the option from the menu or use the keyboard shortcut.

  • Click on the Run menu

  • Select Start Debugging

  • Alternatively, press F5

Press enter or click to view image in full size

When this action is triggered, VS Code begins preparing the application for execution.

Step 2: Select the Debugger

When prompted, choose the C# debugger option. This ensures that the debugging session is configured correctly for a .NET application.

Press enter or click to view image in full size

Step 3: Select the Startup Configuration

Next, choose the option labeled “C#: Launch Startup Project”. This configuration ensures that the correct project is executed as the entry point of your application.

Selecting this option will:

  • Start the Web API project

  • Attach the debugger automatically

  • Launch the application on a local development server

Press enter or click to view image in full size

Working with Breakpoints

To set a breakpoint:

  1. Open a controller file such as WeatherForecastController.cs

  2. Click in the margin next to a line number

  3. A red indicator will appear, marking the breakpoint

Debugging Tools in VS Code

VS Code provides several built-in tools to assist during debugging:

  • Variables panel: Displays current variable values

  • Watch panel: Allows tracking specific expressions

  • Call Stack: Shows the sequence of method calls

  • Step Over (F10): Executes the next line of code

  • Step Into (F11): Enters into method calls

  • Continue (F5): Resumes execution until the next breakpoint

Press enter or click to view image in full size

Conclusion

Running and debugging a .NET Core in VS Code involves a clear sequence of steps, from opening the project to initiating debugging and testing endpoints. With the proper setup, the process is efficient and allows developers to quickly inspect and troubleshoot their applications.

FAQs

Do I need to create a launch.json file?

Not in every case, it is not required in all the cases to create a launch.json file. The development environment can use the existing project configuration and profiles defined in launchSettings.json to run and debug the application.

Can launchSettings.json be used for debugging?

Yes, VS Code can use these profiles when starting the application.

What happens if my solution has multiple projects?

VS Code can detect them and prompt you to select which project you want to run. After selection, the debugger uses the corresponding project configuration and available profiles.

When would launch.json be useful?

launch.json is useful when you need additional control over how debugging is started, such as specifying custom arguments, environment variables, or special debugging scenarios that are not covered by the project’s existing configuration.

More Articles from my Account