[Route("api/articles")]
public class ArticleController : Controller
{
[HttpGet("")]
public IActionResult GetAll() {}
[HttpGet("{id:int}")]
public IActionResult GetById(int id) {}
}
Final Definition
Routing in ASP.NET Core is the system that connects URLs to the right code (controller/action) so the application can respond correctly.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Simple Understanding
When a user hits:
Routing decides:
How Routing Works
Flow:
Example:
Mapped to:
Types of Routing in ASP.NET Core
There are mainly 2 types:
1. Conventional Routing
Defined centrally in
Program.csExample:
How it works:
Example URL:
Calls:
Pros:
Cons:
2. Attribute Routing
Defined directly on controllers/actions using attributes
Example:
URL:
Shortcut:
Pros:
Cons:
3. Endpoint Routing (Modern Approach)
Introduced in ASP.NET Core 3+
Configured in:
Works with:
4. Minimal API Routing (Advanced)
Used in lightweight APIs:
Route Parameters
Example:
controller→ requiredaction→ requiredid?→ optionalRoute Constraints
Restrict parameter types:
Default Values
Routing vs URL Rewriting
Best approach:
Common Mistakes
1. Conflicting routes
2. Missing
app.MapControllers()3. Overusing hardcoded routes
Real Example (Best Practice)
Final Definition