Routing in Controllers — The Map That Guides Every Request!
When you build a web app, one question always appears in the beginner’s mind: “How does ASP.NET Core know which controller method to run?”
The answer is simple — ⭐ Routing in Controllers.
First of all, Routing is like a GPS for your application. In simple terms, it decides which URL should go to which action method. Because of routing, when you type /home/about, your app knows exactly what to do.
As we explore this topic together, you’ll see how easy routing truly is. By the end, you will confidently handle all basic and custom routing rules. So relax, grab a cup of tea ☕, and let’s walk through Routing Examples step by step.
📚 What You Are Going to Learn in This Lesson
✔️ What routing means in ASP.NET Core
✔️ How default routing works
✔️ How attribute routing works
✔️ How to pass values through URLs
✔️ How to create custom routing patterns
✔️ Small working programs with clean output
💡 Simple Explanation
First of all, Routing connects URLs to controller actions.
Next, it checks the matching pattern.
Once this is done, the correct action method runs.
In other words, routing is just a rule book that tells ASP.NET Core:
👉 “If someone visits this URL, run this method.”
For example:/products/list → calls ProductsController.List()/blog/details/10 → calls BlogController.Details(10)
Because of this, routing becomes one of the most important skills in controller learning.
🌍 Real-world Scenario
Imagine you have a small online fruit store 🍎🍌.
When someone visits:
🔗 /fruits/all
Your app must show all fruits.
When someone visits:
🔗 /fruits/details/5
Your app must show fruit with ID = 5.
Routing is the rule that decides:
Which controller handles this and which method should run.
In contrast, without routing, every request would be confused and lost — like a customer walking in a shop with no labels on shelves. 😄
🧩 Syntax + Explanation
⭐ Default Routing (Convention-Based Routing)
ASP.NET Core uses default routing defined in Program.cs:
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
Breakdown
👉 {controller=Home} — If no controller is given, go to HomeController
👉 {action=Index} — If no action is given, run Index()
👉 {id?} — Optional parameter
Example URL:
/Products/List/10
This runs:
- Controller: Products
- Action: List
- id = 10
⭐ Attribute Routing (Routing Defined in Controller)
You can put routing directly above your action methods.
Example 1: Simple Attribute Route
public class ProductsController : Controller
{
[Route("all-products")]
public string All()
{
return "Showing all products 🛒";
}
}
Output:
Showing all products 🛒
Now when you visit:
👉 /all-products
this method runs.
Example 2: Route with Parameters
public class ProductsController : Controller
{
[Route("product/details/{id}")]
public string Details(int id)
{
return $"Product details for ID: {id}";
}
}
Visit:
/product/details/7
Output:
Product details for ID: 7
Explanation
👉 {id} — This part captures value from the URL
👉 It passes it to the action method parameter
Example 3: Multiple Routes for Same Method
public class BlogController : Controller
{
[Route("blog/{id}")]
[Route("post/{id}")]
public string Show(int id)
{
return $"Showing blog post with ID: {id}";
}
}
Visit:
/blog/3
or
/post/3
Output:
Showing blog post with ID: 3
This is useful when you want friendly URLs.
🔧 Small Working Program
Let’s write a quick demo that shows both default and attribute routing.
public class DemoController : Controller
{
// Default: /Demo/Hello
public string Hello()
{
return "Hello from default routing 🙂";
}
// Attribute: /hi
[Route("hi")]
public string Hi()
{
return "Hello from attribute routing 👋";
}
}
Outputs
Visiting /Demo/Hello returns:
Hello from default routing 🙂
Visiting /hi returns:
Hello from attribute routing 👋
➕ Additional Notes
- Additionally, attribute routing gives you full control over URLs.
- Furthermore, combining default + attribute routing is common.
- On top of that, clean URL design improves SEO and user experience.
- Also keep in mind, always use lowercase, short URLs for clarity.
➡️ Next what?
You’ve made amazing progress today! ⭐
You learned how routing works, how default routes behave, and how to create custom URL patterns using attributes.
Now your controllers can react exactly the way you want.
👉 In the next chapter, you will learn Handling HTTP Requests (GET, POST, PUT, DELETE).
This will take your controller skills to the next level. Ready? 😄
