Building RESTful APIs with ASP.NET Core Web API

๐ŸŒ Building RESTful APIs with ASP.NET Core Web API

ASP.NET Core is a modern, open-source, cross-platform framework by Microsoft for building web applications, including powerful and scalable RESTful APIs.


This guide walks you through the basics of creating your first REST API using ASP.NET Core Web API.


๐Ÿ“ฆ What is a RESTful API?

A RESTful API is a web service that follows REST (Representational State Transfer) principles, allowing communication between clients and servers using standard HTTP methods:


HTTP Method Purpose Example Endpoint

GET Read data /api/products

POST Create data /api/products

PUT Update data /api/products/{id}

DELETE Delete data /api/products/{id}


๐Ÿ› ️ Prerequisites

.NET SDK


Visual Studio or Visual Studio Code


C# basics


๐Ÿงฑ Step-by-Step: Create a Web API Project

1. Create the Project

bash

Copy

Edit

dotnet new webapi -n MyApi

cd MyApi

This creates a basic Web API template.


2. Understand the Structure

Key files:


Program.cs & Startup.cs (or just Program.cs in .NET 6+): App configuration


Controllers/WeatherForecastController.cs: Sample controller


appsettings.json: Configuration settings


3. Create a Model

csharp

Copy

Edit

public class Product

{

    public int Id { get; set; }

    public string Name { get; set; }

    public decimal Price { get; set; }

}

4. Create a Controller

csharp

Copy

Edit

using Microsoft.AspNetCore.Mvc;

using System.Collections.Generic;

using System.Linq;


[ApiController]

[Route("api/[controller]")]

public class ProductsController : ControllerBase

{

    private static List<Product> _products = new List<Product>

    {

        new Product { Id = 1, Name = "Laptop", Price = 999.99M },

        new Product { Id = 2, Name = "Mouse", Price = 19.99M }

    };


    [HttpGet]

    public ActionResult<IEnumerable<Product>> Get() => _products;


    [HttpGet("{id}")]

    public ActionResult<Product> Get(int id)

    {

        var product = _products.FirstOrDefault(p => p.Id == id);

        if (product == null) return NotFound();

        return product;

    }


    [HttpPost]

    public ActionResult<Product> Post(Product product)

    {

        product.Id = _products.Max(p => p.Id) + 1;

        _products.Add(product);

        return CreatedAtAction(nameof(Get), new { id = product.Id }, product);

    }


    [HttpPut("{id}")]

    public IActionResult Put(int id, Product updated)

    {

        var product = _products.FirstOrDefault(p => p.Id == id);

        if (product == null) return NotFound();

        product.Name = updated.Name;

        product.Price = updated.Price;

        return NoContent();

    }


    [HttpDelete("{id}")]

    public IActionResult Delete(int id)

    {

        var product = _products.FirstOrDefault(p => p.Id == id);

        if (product == null) return NotFound();

        _products.Remove(product);

        return NoContent();

    }

}

๐Ÿš€ Running the API

bash

Copy

Edit

dotnet run

Navigate to https://localhost:5001/swagger to see the Swagger UI (interactive API docs).


๐Ÿงฐ Bonus Features in ASP.NET Core Web API

๐Ÿงช Swagger/OpenAPI: Built-in with template for API testing and docs.


๐Ÿ” Authentication & Authorization: Supports JWT, OAuth, Identity.


๐Ÿ“ฆ Dependency Injection: Built-in and powerful.


๐Ÿงต Middleware Pipeline: Easy to add custom logging, error handling, etc.


๐Ÿ—‚️ Entity Framework Core: For database integration.


✅ Final Tips

Follow REST naming conventions (/api/resources)


Use proper HTTP status codes (200 OK, 201 Created, 404 Not Found, etc.)


Use DTOs for data transfer and validation


Keep logic out of controllers (use services)


Would you like a complete project template, database integration example, or testing guide next?

Learn Full Stack Dot NET Training in Hyderabad

Read More

Understanding MVC (Model-View-Controller) Architecture in .NET

Introduction to Back-End Development with ASP.NET Core

Visit Our Quality Thought Training in Hyderabad

Get Directions


Comments

Popular posts from this blog

Entry-Level Cybersecurity Jobs You Can Apply For Today

Understanding Snowflake Editions: Standard, Enterprise, Business Critical

Installing Tosca: Step-by-Step Guide for Beginners