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

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

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:

  • HTTPS: https://localhost:5001
  • HTTP: https://localhost:5000

Test the API

  • Open a browser and go to: https://localhost:5001/weatherforecast
  • Or use Postman to make a GET request to https://localhost:5001/weatherforecast

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!

要查看或添加评论,请登录

Chandan Kumar的更多文章

社区洞察

其他会员也浏览了