Introduction
ASP.NET Core is a modern framework used to build fast, secure, and scalable web applications and APIs. A Web API allows different applications, such as websites, mobile apps, and desktop software, to communicate with each other using HTTP.
In simple words, a Web API acts as a bridge between a client and a server. In this article, you will learn how to create a Web API in ASP.NET Core step by step, using simple language and practical examples suitable for beginners.
What Is a Web API?
A Web API is a service that exposes data and functionality over the internet using standard HTTP methods such as GET, POST, PUT, and DELETE.
Web APIs are commonly used for:
Example: A weather application calls a Web API to get temperature data and displays it on the screen.
Prerequisites to Create an ASP.NET Core Web API
Before creating a Web API, you need a few basic things:
Basic knowledge of C# programming
.NET SDK installed on your system
A code editor such as Visual Studio or Visual Studio Code
Example: Once the .NET SDK is installed, you can create and run API projects from your system.
Creating a New ASP.NET Core Web API Project
The first step is to create a new Web API project.
You can create a project using the command line:
dotnet new webapi -n SampleWebApi
cd SampleWebApi
dotnet run
This command creates a basic Web API project with default configuration.
Example: When you run the project, a sample endpoint is available that returns weather data.
Understanding the Project Structure
An ASP.NET Core Web API project contains several important files and folders:
Program.cs – Application startup and configuration
Controllers – Contains API controllers
appsettings.json – Application settings
Properties – Launch settings
Example: Controllers are the heart of a Web API because they handle incoming requests and return responses.
Creating a Simple API Controller
A controller is a class that handles HTTP requests. Let’s create a simple controller.
Create a file named HelloController.cs inside the Controllers folder:
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class HelloController : ControllerBase
{
[HttpGet]
public string GetMessage()
{
return "Hello from ASP.NET Core Web API";
}
}
Example: When you open /api/hello in the browser, you will see a greeting message.
Using HTTP Methods in Web API
ASP.NET Core Web API supports different HTTP methods for different operations.
Example of POST method:
[HttpPost]
public string CreateMessage(string message)
{
return "Message received: " + message;
}
These methods help create RESTful APIs that follow standard web practices.
Routing in ASP.NET Core Web API
Routing defines how URLs map to controller actions.
Example:
[HttpGet("welcome")]
public string Welcome()
{
return "Welcome to Web API";
}
This method will respond to /api/hello/welcome.
Routing makes APIs clean, readable, and easy to use.
Returning Data as JSON
Web APIs usually return data in JSON format. ASP.NET Core automatically converts objects into JSON.
Example:
[HttpGet("user")]
public object GetUser()
{
return new { Id = 1, Name = "Rahul", City = "Delhi" };
}
This response can be easily consumed by frontend or mobile applications.
Testing the Web API
You can test your Web API using:
Browser
API testing tools
Command-line tools
Example: Open the API URL in the browser to see GET responses or use a testing tool to send POST requests.
Testing ensures your API works correctly before deployment.
Best Practices for Beginners
When creating Web APIs, follow these simple best practices:
Example: Clear API design helps other developers understand and use your API easily.
CRUD Web API Example Using In-Memory Data
A CRUD Web API allows you to Create, Read, Update, and Delete data. For beginners, it is best to start with in-memory data instead of a database.
First, create a simple model class:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Now create a controller that stores data in a static list:
[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
private static List<Product> products = new List<Product>
{
new Product { Id = 1, Name = "Laptop", Price = 50000 },
new Product { Id = 2, Name = "Mobile", Price = 20000 }
};
[HttpGet]
public List<Product> GetAll()
{
return products;
}
[HttpGet("{id}")]
public Product GetById(int id)
{
return products.FirstOrDefault(p => p.Id == id);
}
[HttpPost]
public Product Create(Product product)
{
product.Id = products.Max(p => p.Id) + 1;
products.Add(product);
return product;
}
[HttpPut("{id}")]
public Product Update(int id, Product updatedProduct)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null) return null;
product.Name = updatedProduct.Name;
product.Price = updatedProduct.Price;
return product;
}
[HttpDelete("{id}")]
public bool Delete(int id)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null) return false;
products.Remove(product);
return true;
}
}
Example: You can now use GET, POST, PUT, and DELETE requests to manage products without using a database.
Extending the Web API with SQL Server and Entity Framework Core
In real-world applications, data must be stored permanently. For this, ASP.NET Core Web APIs commonly use SQL Server with Entity Framework Core.
First, install the required packages:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
Create a database context:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options) { }
public DbSet<Product> Products { get; set; }
}
Configure the database connection in Program.cs:
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
Add the connection string in appsettings.json:
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=ProductDb;Trusted_Connection=True;"
}
Update the controller to use the database:
public class ProductsController : ControllerBase
{
private readonly AppDbContext _context;
public ProductsController(AppDbContext context)
{
_context = context;
}
[HttpGet]
public List<Product> GetAll()
{
return _context.Products.ToList();
}
[HttpPost]
public Product Create(Product product)
{
_context.Products.Add(product);
_context.SaveChanges();
return product;
}
}
Example: Now product data is stored in SQL Server and remains available even after restarting the application.
This approach is commonly used in production-grade APIs.
Summary
Creating a Web API in ASP.NET Core is simple and beginner-friendly. By understanding project structure, controllers, routing, and HTTP methods, you can build powerful REST APIs using C#. ASP.NET Core provides excellent performance, automatic JSON handling, and modern development features, making it a great choice for backend and API development in 2026.