MVC URL Routing
- Sudherson V
- May 19, 2015
- 1 min read

MVC URL Routing system allows to create powerful and flexible URLs for our applications. It provides two functionalities,
Inspect incoming URLs - This functionality manifests when the application receives a client request.
Generate outgoing URLs - This functionality manifests when we render URLs using Url.Action in our Views.
Inspect incoming URLs
Static URL segments:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("AgentRoute", "Agent/Customer",
new { controller = "Customer", action = "Index" });
}
URL http://localhost:27424/Agent/Customer/ will launch controller = "Customer", action = "Index".
routes.MapRoute("XRoute", "X{controller}/{action}",
new { controller = "Customer", action = "Index" });
URLs http://localhost:27424/XCustomer/Index, http://localhost:27424/XCustomer/, will launch controller = "Customer", action = "Index".
routes.MapRoute("BrokerRoute", "Broker/{controller}/{action}",
new { controller = "Customer", action = "Index" });
URLs http://localhost:27424/Broker/Customer/Index, http://localhost:27424/Broker/Customer/, http://localhost:27424/Broker/, will launch controller = "Customer", action = "Index".
Generate outgoing URLs
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("ColumnPage", "SortBy{sortBy}/Page{page}",
new { controller = "Log", action = "Index" });
}
@Url.Action("Index", "Log", new { page = x, sortBy = y }) will generate URL http://localhost:27424/SortByy/Pagex
routes.MapRoute("Column", "SortBy{sortBy}",
new { controller = "Log", action = "Index" });
@Url.Action("Index", "Log", new { sortBy = x }) will generate URL http://localhost:27424/SortByx