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
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
Download Visual Studio Code from: π VS Code Download
Install VS Code and open it.
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:
http://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
Open VS Code settings (
Ctrl + ,
).Search for βsearch.excludeβ.
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
Open
API
project in VS Code.Press
F5
to start debugging.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!