Complete Step-by-Step Guide to Setting Up and Running a.NET Web API

Complete Step-by-Step Guide to Setting Up and Running a.NET Web API

Detailed Instructions on How to Set Up and Operate a.NET Web API

Β·

3 min read

This guide covers the entire process of installing .NET, setting up a Web API, configuring Visual Studio Code, and running the API.

Step 1: Install .NET SDK

First, download and install the .NET SDK from the official .NET website:

πŸ”— Download .NET SDK

After installation, verify the installation by running:

dotnet --version

This command should return the installed .NET version (e.g., 8.0.100).

Step 2: Install Visual Studio Code

  1. Download Visual Studio Code from: πŸ”— VS Code Download

  2. Install VS Code and open it.

  3. Install the following extensions inside VS Code:

  • C# (by Microsoft) β€” Provides IntelliSense & debugging.

  • C# Dev Kit β€” Improves .NET development in VS Code.

  • Material Icon Theme β€” Adds better file icons.

  • NuGet Package Manager β€” Helps manage dependencies.

πŸ‘‰ To install extensions, open VS Code and press Ctrl+Shift+X, then search for and install each extension.

Step 3: Create a New .NET Web API Project

Now, let’s create a .NET Web API project step by step.

1. Open Terminal in VS Code

  • Open VS Code.

  • Press Ctrl + ~ to open the terminal.

2. Create a New Solution

Run the following command:

dotnet new sln -n MyWebAPI

This creates a solution file (MyWebAPI.sln), which helps manage multiple projects.

3. Create a Web API Project

Now, create a new Web API project inside a folder named API:

dotnet new webapi -o API

This will generate a project with default controller-based API structure.

4. Add API Project to Solution

Navigate back to the solution folder and add the API project:

cd MyWebAPI
dotnet sln add API/

Your solution now manages the API project.

Step 4: Understanding the Folder Structure

After the commands above, your project structure looks like this:

MyWebAPI/
│── MyWebAPI.sln          // Solution file
β”‚
└── API/                  // Web API project
    │── Controllers/       // API Controllers
    β”‚   └── WeatherForecastController.cs
    β”‚
    │── Properties/        // Launch settings for debugging
    β”‚
    │── appsettings.json   // Configuration file
    │── Program.cs         // Main entry point
    │── Startup.cs (if using .NET 6 or earlier)
    │── API.csproj         // Project file
    │── bin/               // Build output (excluded)
    │── obj/               // Build cache (excluded)

Step 5: Run the Web API

To start your Web API, run:

dotnet run --project API

OR, for live reload on changes, use:

dotnet watch --project API run

By default, the API runs on:

Test the API

Step 6: Configure VS Code Settings

Exclude bin/ and obj/ from Search

  1. Open VS Code settings (Ctrl + ,).

  2. Search for β€œsearch.exclude”.

  3. Add the following:

"**/obj": true,
"**/bin": true

This prevents unnecessary files from showing in search results.

Step 7: Install and Manage Dependencies

If you need additional packages, use NuGet. For example, to install Entity Framework Core, run:

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools

To list installed packages:

dotnet list package

Step 8: Debugging in VS Code

  1. Open API project in VS Code.

  2. Press F5 to start debugging.

  3. VS Code will launch the API and attach the debugger.

Step 9: Publish Your API

To publish the Web API:

dotnet publish --configuration Release --output ./publish

This creates a publish-ready version in the publish folder.

Final Notes

βœ… You have successfully:

  • Installed .NET & VS Code

  • Created a new Web API project

  • Configured VS Code & extensions

  • Ran the API & tested endpoints

  • Set up debugging and publishing

πŸŽ‰ Congratulations! Your .NET Web API is ready to go!