Complete Step-by-Step Guide to Setting Up and Running a.NET Web API
Chandan Kumar
Full-Stack Developer | Passionate about innovative web solutions! Angular Developer | MEAN Stack Developer
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:
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
?? 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
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
"**/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
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:
?? Congratulations! Your .NET Web API is ready to go!