In the modern web, RESTful APIs are the bridge between frontend and backend, enabling seamless communication between apps, devices, and services. A hands-on REST API with .NET Core approach makes building robust and scalable APIs easier than ever. In this guide, we’ll walk you through creating your first RESTful API with .NET Core step by step.
What is a RESTful API?
A RESTful API (Representational State Transfer) is a web service that follows certain conventions:
- Uses standard HTTP methods: GET, POST, PUT, DELETE
- Is stateless, meaning each request contains all necessary info
- Organizes resources with URLs, e.g.,
/api/products
Think of it as a well-structured menu: each URL is a “resource,” and HTTP methods define actions on that resource.
Building Your First .NET Core Web API
Follow these five key steps to build, structure, and test your first RESTful API in .NET Core effectively.
Step 1: Set Up Your .NET Core Project
- Install .NET SDK
- Download the latest .NET SDK from Microsoft.
- Create a New Web API Project
- Open your terminal or Visual Studio and run:
- Run the Application
Open https://url:5001/swagger to see Swagger UI, which automatically generates API documentation and allows you to test endpoints.
Step 2: Understand the Project Structure
Controllers/
Handle HTTP requests.
Models/
Define data structures.
Define data structures
Configures app and services.
appsettings.json
Application configuration
Step 3: Create Your First API
- Define a Model
- Create
Models/Product.cs:
- Create
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
- Add a Controller
- Create
Controllers/ProductController.cs:
- Create
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
[ApiController]
[Route("api/[controller]")]
public class ProductController : ControllerBase
{
private static List<Product> products = new List<Product>
{
new Product { Id = 1, Name = "Laptop", Price = 1200 },
new Product { Id = 2, Name = "Smartphone", Price = 800 }
};
[HttpGet]
public IActionResult Get() => Ok(products);
[HttpGet("{id}")]
public IActionResult Get(int id)
{
var product = products.Find(p => p.Id == id);
return product == null ? NotFound() : Ok(product);
}
[HttpPost]
public IActionResult Post(Product product)
{
products.Add(product);
return CreatedAtAction(nameof(Get), new { id = product.Id }, product);
}
[HttpPut("{id}")]
public IActionResult Put(int id, Product updatedProduct)
{
var product = products.Find(p => p.Id == id);
if (product == null) return NotFound();
product.Name = updatedProduct.Name;
product.Price = updatedProduct.Price;
return NoContent();
}
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
var product = products.Find(p => p.Id == id);
if (product == null) return NotFound();
products.Remove(product);
return NoContent();
}
}
Step 4: Test Your API
- Swagger UI – Navigate to https://url:5001/swagger.
- Postman – Send GET, POST, PUT, DELETE requests.
- Command Line with curl: curl https://url:5001/api/product
curl https://url:5001/api/product
Step 5: REST API Best Practices
- Use HTTP Status Codes – Communicate success, errors, or not found.
- Follow Resource Naming Conventions – Plural nouns, e.g.,
/api/products. - Enable Logging & Error Handling – Use middleware like
UseExceptionHandler. - Use Dependency Injection – Makes your API modular and testable.
- Secure Your API – HTTPS, JWT tokens, or OAuth2 for authentication.
Conclusion
Creating RESTful APIs with .NET Core is simple yet powerful. By following the steps above, you can build scalable APIs, integrate with databases, and extend your project with authentication, caching, and more. Start small, adhere to REST principles, and watch your API grow into a robust backend service.
