Introduction
For anyone learning ASP.NET Core, understanding controllers, routes, and endpoints is crucial. These are the building blocks of a web API or MVC application.
Controllers handle incoming requests, routes define how requests reach controllers, and endpoints determine the response sent back to clients. A proper understanding of these concepts helps you design clean, maintainable, and efficient web applications.
This article explains these concepts in a step-by-step, beginner-friendly manner, with examples suitable for beginners and web developers building APIs or full-stack applications.
What is a Controller?
A controller in ASP.NET Core is a class that handles HTTP requests. It acts as a bridge between the client and the server, processing requests, executing logic, and returning responses.
Controllers can be used in two main types of projects:
MVC Applications – Controllers return views or pages.
Web API Applications – Controllers return data in JSON, XML, or other formats.
Creating a Simple Controller
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
[HttpGet]
public IActionResult GetUsers()
{
var users = new string[] { "John", "Jane", "Rahul" };
return Ok(users);
}
}
Explanation
[ApiController]– Marks the class as a Web API controller, enabling automatic model validation and response formatting.[Route("api/[controller]")]– Sets the route for this controller.[controller]is replaced by the controller’s name without the “Controller” suffix, i.e.,Users.GetUsers()– Handles GET requests and returns a list of users.
Understanding Routes in ASP.NET Core
A route defines the URL pattern that maps to a controller and its actions. ASP.NET Core supports two types of routing:
Conventional Routing
Attribute Routing
1. Conventional Routing
Conventional routing is defined in Program.cs or Startup.cs and follows a predefined pattern:
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
{controller=Home}– Default controller isHomeif none is specified.{action=Index}– Default action method isIndex.{id?}– Optional parameterid.
Example URL
/Users/GetUsersMaps to UsersController.GetUsers().
2. Attribute Routing
Attribute routing uses attributes directly on controllers and action methods to define routes:
[ApiController]
[Route("api/users")]
public class UsersController : ControllerBase
{
[HttpGet("all")]
public IActionResult GetAllUsers() => Ok(new[] { "John", "Jane" });
[HttpGet("{id}")]
public IActionResult GetUserById(int id) => Ok($"User {id}");
}
[HttpGet("all")]→ Responds toGET /api/users/all.[HttpGet("{id}")]→ Responds toGET /api/users/1,GET /api/users/2, etc.
Understanding Endpoints
An endpoint is the final destination of a request in ASP.NET Core. It represents a combination of:
HTTP method (GET, POST, PUT, DELETE)
Route (URL pattern)
Controller action that executes the request
ASP.NET Core maps requests to endpoints using middleware and routing configurations.
Example of Multiple Endpoints
[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
[HttpGet]
public IActionResult GetAllProducts() => Ok(new[] { "Laptop", "Phone" });
[HttpPost]
public IActionResult AddProduct([FromBody] string product) => Ok($"Added {product}");
[HttpGet("{id}")]
public IActionResult GetProductById(int id) => Ok($"Product {id}");
}
Endpoints

Join the conversation! Your thoughts help the community grow.