Curriculum
Routing in ASP.NET Core MVC is the mechanism that determines how incoming URLs are mapped to controllers and action methods. Every ASP.NET Core MVC application uses Routing in ASP.NET Core MVC to identify which controller should handle a request and which action method should execute.
Understanding Routing in ASP.NET Core MVC is essential because navigation, page access, form processing, API communication, and URL management all depend on routing. Whether you are building a School Management System, Hospital Management System, E-Commerce Platform, Banking Portal, CRM Solution, ERP Software, or Enterprise Application, routing is one of the core components of application architecture.
Routing is the process of matching a URL with a controller and an action method.
Example:
https://example.com/
Student/Details/1
ASP.NET Core analyzes the URL and determines:
Controller
↓
Action
↓
Parameters
The appropriate code is then executed.
Without Routing:
No URL Processing
No Page Navigation
No Request Mapping
Routing enables users to access different parts of an application.
Request:
/Student/Details/1
ASP.NET Core determines:
Controller:
StudentController
Action:
Details
Parameter:
1
The matching action method is executed.
User Request
↓
Routing Engine
↓
Controller
↓
Action Method
↓
Response
Routing acts as the traffic manager of the application.
Most applications use:
app.MapControllerRoute(
name: "default",
pattern:
"{controller=Home}/
{action=Index}/
{id?}");
This is the standard MVC route.
Example:
{controller=Home}
Default Controller:
HomeController
If no controller is specified, HomeController is used.
Example:
{action=Index}
Default Action:
Index()
If no action is specified, Index executes automatically.
Example:
{id?}
The question mark means:
Optional Value
The URL can work with or without the parameter.
URL:
/Student/Details/10
ASP.NET Core Maps:
Controller:
StudentController
Action:
Details
Id:
10
The Details action receives the value.
public class
StudentController :
Controller
{
public IActionResult
Details(int id)
{
return Content(
$"Student {id}");
}
}
Output:
Student 10
The route parameter is automatically bound.
The default routing system is known as:
Conventional Routing
Example:
app.MapControllerRoute(
name: "default",
pattern:
"{controller=Home}/
{action=Index}/
{id?}");
Most MVC applications use this approach.
ASP.NET Core also supports:
Attribute Routing
Routes are defined directly on controllers and actions.
Controller:
[Route("students")]
public class
StudentController :
Controller
{
}
URL:
/students
The route is explicitly defined.
Example:
[Route("students/details")]
public IActionResult
Details()
{
return View();
}
URL:
/students/details
The action executes directly.
Example:
[Route(
"students/{id}")]
public IActionResult
Details(int id)
{
return Content(
$"Student {id}");
}
URL:
/students/5
Output:
Student 5
Parameters are automatically passed.
Example:
[Route(
"students/{id}/
course/{courseId}")]
public IActionResult
Details(
int id,
int courseId)
{
return Content(
$"{id} {courseId}");
}
URL:
/students/1/course/10
Output:
1 10
Multiple values can be captured.
Constraints restrict parameter values.
Example:
[Route(
"students/{id:int}")]
Only integers are accepted.
Valid:
/students/5
Invalid:
/students/rahul
Constraints improve reliability.
{id:int}
{name}
{id:guid}
{name:minlength(3)}
These help validate URLs.
Example:
[Route(
"students/details",
Name="StudentDetails")]
Named routes simplify URL generation.
Example:
Url.Action(
"Details",
"Student")
Output:
/Student/Details
URLs can be generated dynamically.
Example:
return RedirectToAction(
"Index",
"Student");
Output:
Redirect User
This is commonly used after form submissions.
Example:
return RedirectToAction(
"Details",
new
{
id = 1
});
Generated URL:
/Student/Details/1
Route values are automatically included.
URL:
/Student/Search?
name=Rahul
Controller:
public IActionResult
Search(
string name)
{
}
Result:
name = Rahul
Query strings are commonly used for filtering and searching.
Bad URL:
/Page?id=10
Good URL:
/students/details/10
SEO-friendly URLs:
Improve Readability
Improve SEO
Improve User Experience
ASP.NET Core routing supports clean URLs.
Student Portal:
/students
/students/details/1
/students/edit/1
/students/delete/1
Routing directs each request to the correct action.
Examples:
/products
/products/details/10
/cart
/orders
Routing enables navigation across the website.
Improves user experience.
Search engines prefer readable URLs.
Users can access pages directly.
Supports conventional and attribute routing.
Suitable for large applications.
These advantages make routing essential.
Can confuse users.
May cause conflicts.
Can lead to invalid requests.
Reduces maintainability.
Use URL generation methods instead.
Routing maps URLs to controllers and action methods.
Conventional Routing uses predefined route patterns.
Attribute Routing defines routes directly on controllers and actions.
A Route Constraint restricts acceptable parameter values.
URL Generation creates URLs dynamically from controllers and actions.
Routing enables navigation and request processing in web applications.
Routing is the process of mapping URLs to controllers and action methods.
Conventional Routing uses a centralized route pattern configuration.
Attribute Routing uses route attributes directly on controllers and actions.
Route Constraints restrict acceptable values for route parameters.
SEO-friendly URLs improve readability and search engine rankings.
Routing enables URL processing, navigation, and request handling in MVC applications.
WhatsApp us