Welcome to another tutorial at Satyarth Programming Hub! If you’ve been following my YouTube channel or exploring my GitHub projects, you know I’m passionate about making programming accessible. Today, we’re diving into the world of Web APIs with .NET Core—a powerful framework for building modern, scalable applications. In this step-by-step guide, I’ll walk you through creating your first .NET Core Web API, perfect for beginners looking to add API development to their skillset. Let’s get started!
What is a Web API?
A Web API (Application Programming Interface) allows different applications to communicate over the internet. Think of it as a waiter in a restaurant—your app sends a request, and the API delivers the data. .NET Core is an excellent choice for building Web APIs because it’s cross-platform, lightweight, and supports modern development practices.
Step 1: Set Up Your Development Environment
First, ensure you have the .NET SDK installed (download it from dotnet.microsoft.com). I recommend using Visual Studio Code with the C# extension for a lightweight setup, or Visual Studio Community if you prefer a full IDE. Once installed, open your terminal and verify the installation:
dotnet --version
This should display the .NET version (e.g., 8.0). If not, revisit the installation steps.
Step 2: Create a New Web API Project
Let’s create a new project. In your terminal, navigate to your desired folder and run:
dotnet new webapi -n MyFirstApi
cd MyFirstApi
This generates a basic Web API project. Open it in your IDE, and you’ll see files like Program.cs and a Controllers folder. Start the project to ensure it works:
dotnet run
Visit https://localhost:5001/swagger (or the port shown) to see the Swagger UI—a tool to test your API.
Step 3: Understand the Default API
The template includes a sample controller, WeatherForecastController.cs. Open it to see a basic GET endpoint:
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
This endpoint returns random weather data. Test it by navigating to /weatherforecast in Swagger—it’s a great starting point to understand how APIs work.
Step 4: Create Your Own API Endpoint
Let’s build a custom endpoint. Create a new controller, ValuesController.cs, in the Controllers folder:
using Microsoft.AspNetCore.Mvc;
namespace MyFirstApi.Controllers;
[ApiController]
[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
var values = new string[] { "Value1", "Value2" };
return Ok(values);
}
[HttpGet("{id}")]
public IActionResult Get(int id)
{
return Ok($"Value {id}");
}
}
- The Get() method returns a list of values.
- The Get(id) method returns a single value based on an ID. Test these endpoints in Swagger (/api/values and /api/values/1).
Step 5: Add a POST Endpoint
Let’s add a POST endpoint to create data. In ValuesController.cs, add:
[HttpPost]
public IActionResult Post([FromBody] string value)
{
if (string.IsNullOrEmpty(value))
return BadRequest("Value cannot be empty");
return CreatedAtAction(nameof(Get), new { id = 1 }, value);
}
Test this in Swagger by sending a POST request to /api/values with a JSON body like {“value”: “NewValue”}. This simulates creating data, a common API task.
Conclusion
Congratulations—you’ve built your first .NET Core Web API! We set up the environment, created a project, explored the default API, added custom GET endpoints, and implemented a POST endpoint. Want the full code? Check out my GitHub repository at github.com/SatyarthProgrammingHub. For more in-depth lessons, visit my Tutorials page or subscribe to my YouTube channel. Have questions or want to share your API project? Drop a comment below or reach out via the Contact page—I’d love to hear from you!