ASP.NET Core  

How to Create Your First Web API Using ASP.NET Core?

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:

  • It receives a request

  • Processes it

  • Sends back a response (usually in JSON format)

For example:

  • A weather app calls an API to get weather data

  • An e-commerce site uses APIs to fetch product details

Why Use ASP.NET Core for Web APIs?

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

  • It is fast and high-performance

  • It supports cross-platform development (Windows, Linux, macOS)

  • It has built-in dependency injection

  • It is easy to test and maintain

  • It supports RESTful API development

Prerequisites

Before you start, make sure you have:

  • .NET SDK installed

  • A code editor like Visual Studio or Visual Studio Code

  • Basic understanding of C# and HTTP methods

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:

  • Program.cs → Entry point of the application

  • Controllers folder → Contains API controllers

  • appsettings.json → Configuration file

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:

  • [ApiController] enables API-specific features

  • [Route] defines the URL path

  • [HttpGet] handles GET requests

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:

  • GET → Retrieve data

  • POST → Create data

  • PUT → Update data

  • DELETE → Remove data

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

  • Keep controllers clean and simple

  • Use services for business logic

  • Use proper HTTP status codes

  • Validate input data

  • Use meaningful route names

Step 10: Next Steps

Once you're comfortable, you can learn:

  • Connecting to a database (Entity Framework Core)

  • Authentication and Authorization (JWT)

  • Logging and Exception Handling

  • API Versioning

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.