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
Routing means: Connecting a URL to a Controller and an Action Method.
Imagine a hospital.
Same flow in MVC:
URL → Routing → Controller → Action → View → Response
Default Route Pattern:
{controller}/{action}/{id}
https://localhost:1234/Student/Details/5
Explanation:
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
This follows the default pattern:
{controller}/{action}/{id}
It is defined inside RouteConfig.cs
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
[Route("student/details/{id}")]
public ActionResult Details(int id)
{
return View();
}
Example URL:
/student/details/10
Here, 10 is passed as the id parameter.
Imagine an online shopping website:
Routing helps to:
Browser URL
↓
Routing System
↓
Controller
↓
Action Method
↓
View
↓
Response to User
✔ Routing connects URL with Controller
✔ Default routing follows a fixed pattern
✔ Attribute routing gives more control
✔ Essential for real-world applications
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
}
);
}
}
}
https://localhost:44369/Home/Index
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
}
);
}
}
}
https://localhost:44369/Employee/Details