Introduction

If you're starting your journey in backend development, learning how to build a Web API is one of the most important skills you can acquire. A Web API allows different applications to communicate with each other over the internet. Whether you're building a mobile app, a web app, or integrating services, APIs are everywhere.

ASP.NET Core is a powerful, fast, and cross-platform framework developed by Microsoft that makes building APIs simple and efficient.

What is a Web API?

A Web API is a set of rules that allows one software application to interact with another over HTTP.

In simple words:

For example:

Why Use ASP.NET Core for Web APIs?

ASP.NET Core is widely used for building APIs because:

Prerequisites

Before you start, make sure you have:

Step 1: Create a New ASP.NET Core Web API Project

You can create a new project using either Visual Studio or the .NET CLI.

Using .NET CLI:

dotnet new webapi -n MyFirstApi
cd MyFirstApi

This command creates a ready-to-run Web API project.

Step 2: Understand the Project Structure

When you open the project, you will see important files:

ASP.NET Core uses a minimal hosting model, meaning everything is configured in Program.cs.

Step 3: Run the Application

Run the project using:

dotnet run

You will see a URL like:

https://localhost:5001

Open it in your browser. You may see Swagger UI, which helps you test APIs easily.

Step 4: Create Your First API Controller

Inside the Controllers folder, create a new file:

using Microsoft.AspNetCore.Mvc;

namespace MyFirstApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class HelloController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Ok("Hello, this is your first Web API!");
        }
    }
}

Explanation:

Step 5: Test Your API

Run the application and open:

https://localhost:5001/api/hello

You will see:

Hello, this is your first Web API!

You can also test it using Swagger UI or tools like Postman.

Step 6: Understanding HTTP Methods

Web APIs use different HTTP methods:

Example:

[HttpPost]
public IActionResult Create(string name)
{
    return Ok($"Created: {name}");
}

Step 7: Returning JSON Data

Most APIs return JSON data. Example:

[HttpGet("user")]
public IActionResult GetUser()
{
    var user = new
    {
        Id = 1,
        Name = "Baibhav",
        Role = "Developer"
    };

    return Ok(user);
}

Output:

{
  "id": 1,
  "name": "Baibhav",
  "role": "Developer"
}

Step 8: Adding Dependency Injection

ASP.NET Core uses dependency injection by default.

Example service:

public interface IMessageService
{
    string GetMessage();
}

public class MessageService : IMessageService
{
    public string GetMessage()
    {
        return "Hello from Service";
    }
}

Register in Program.cs:

builder.Services.AddSingleton<IMessageService, MessageService>();

Use in controller:

private readonly IMessageService _service;

public HelloController(IMessageService service)
{
    _service = service;
}

[HttpGet("service")]
public IActionResult GetMessage()
{
    return Ok(_service.GetMessage());
}

Step 9: Best Practices for Beginners

Step 10: Next Steps

Once you're comfortable, you can learn:

Summary

Creating your first Web API using ASP.NET Core is easier than you might think. With minimal setup and simple code, you can build powerful APIs that serve data to web and mobile applications. By understanding controllers, routing, HTTP methods, and dependency injection, you have already taken a big step into backend development. Keep practicing and gradually move towards advanced topics like database integration and security to become a complete API developer.