Routing in ASP.NET MVC


What is Routing?

Routing is a mechanism that decides:

👉 Which Controller should handle the request
👉 Which Action Method should execute
👉 Based on the URL entered in the browser


Simple Definition

Routing means: Connecting a URL to a Controller and an Action Method.


Real-Life Example

Imagine a hospital.

  • You enter the hospital (URL)
  • The receptionist sends you to the correct department (Controller)
  • The doctor treats you (Action Method)
  • You get treatment (Response/View)

Same flow in MVC:

URL → Routing → Controller → Action → View → Response


Basic Routing Structure

Default Route Pattern:

{controller}/{action}/{id}

Example URL

https://localhost:1234/Student/Details/5

Explanation:

  • Student → Controller
  • Details → Action Method
  • 5 → ID Parameter

Default Route Configuration (RouteConfig.cs)

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new 
    { 
        controller = "Home",action = "Index",id = UrlParameter.Optional 
    }
);

If the user types only:
https://localhost:1234/

It automatically goes to:
Home Controller → Index Action


Types of Routing

  • 1️⃣ Convention-Based Routing (Default Routing)
  • 2️⃣ Attribute Routing

1️⃣ Convention-Based Routing (Basic)

This follows the default pattern:

{controller}/{action}/{id}

It is defined inside RouteConfig.cs


2️⃣ Attribute Routing (Advanced)

Here, we define routes directly above the Action Method.

[Route("students/all")]
public ActionResult GetAllStudents()
{
    return View();
}

Now the URL becomes:
https://localhost:1234/students/all


Parameter Routing Example

[Route("student/details/{id}")]
public ActionResult Details(int id)
{
    return View();
}

Example URL:
/student/details/10

Here, 10 is passed as the id parameter.


Advanced Real-Life Example (E-Commerce Website)

Imagine an online shopping website:

  • /products
  • /products/electronics
  • /products/electronics/5

Routing helps to:

  • Show all products
  • Show products by category
  • Show single product details

Routing Flow Diagram

Browser URL
↓ Routing System
↓ Controller
↓ Action Method
↓ View
↓ Response to User


Important Interview Points

  • Routing maps URL to Controller and Action
  • Default route pattern: {controller}/{action}/{id}
  • Attribute routing allows custom URLs
  • ID parameter is optional
  • Routing improves clean and meaningful URLs

Summary

✔ Routing connects URL with Controller
✔ Default routing follows a fixed pattern
✔ Attribute routing gives more control
✔ Essential for real-world applications


AIT Image

AIT Image

Default File

RouteConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Your_Project_Name
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
               routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new 
                { 
                    controller = "Home",action = "Index",id = UrlParameter.Optional 
                }
            );
        }
    }
}

Application URL

https://localhost:44369/Home/Index

Example

RouteConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Your_Project_Name
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
               routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new 
                { 
                    controller = "Employee",action = "Details",id = UrlParameter.Optional 
                }
            );
        }
    }
}

Application URL

https://localhost:44369/Employee/Details