Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the three segments of the default route, that is present in ASP .Net MVCnC#?
The ASP.NET MVC Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. When the ASP.NET MVC application launches, it registers one or more patterns with the framework's route table to tell the routing engine what to do with any requests that match those patterns. When the routing engine receives a request at runtime, it matches that request's URL against the URL patterns registered with it and gives the response according to a pattern match.
ASP.NET introduced Routing to eliminate the need of mapping each URL with a physical file. Routing enables us to define a URL pattern that maps to the request handler. The MVC framework leverages routing to direct a request to a controller. The Global.asax file is where we define the route for our application.
Default Route Configuration
Below is the route config of an MVC application −
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 }
);
}
}
Three Segments of Default Route
The default route pattern "{controller}/{action}/{id}" contains three segments that define how URLs are mapped to controllers and actions −
1. Controller Segment
The first segment {controller} maps to the controller class name. When a URL like /Products/Details/5 is requested, the routing engine looks for a controller named ProductsController. If no controller segment is provided, it defaults to "Home".
2. Action Segment
The second segment {action} maps to the action method within the controller. From the same URL example, it would call the Details method in the ProductsController. If no action is specified, it defaults to "Index".
3. Id Segment
The third segment {id} is an optional parameter that gets passed to the action method. In our example URL, the value 5 would be passed as a parameter to the Details action method. This segment is marked as UrlParameter.Optional in the route configuration.
Example of Route Mapping
using System;
using System.Web.Mvc;
public class ProductsController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Details(int? id)
{
if (id.HasValue)
{
ViewBag.Message = $"Product ID: {id}";
}
else
{
ViewBag.Message = "No ID provided";
}
return View();
}
}
// URL Examples:
// /Products ? ProductsController.Index()
// /Products/Details ? ProductsController.Details(null)
// /Products/Details/5 ? ProductsController.Details(5)
Route Matching Examples
| URL | Controller | Action | Id |
|---|---|---|---|
| / | Home | Index | null |
| /Products | Products | Index | null |
| /Products/Details | Products | Details | null |
| /Products/Details/5 | Products | Details | 5 |
Conclusion
The three segments of the default ASP.NET MVC route are Controller, Action, and Id. These segments provide a clean URL structure that maps directly to controller classes and their action methods, with the Id segment serving as an optional parameter for passing data to the action method.
