URL Routing in MVC
URL Routing:-
Generally, URL Routing/Rewiring is a URL pattern matching system that monitor the browser request and heck out what to do with that browser request. The Routing module is responsible for mapping incoming browser requests to particular ASP.NET MVC controller actions. We can to make very pretty URLs more flexible. We can create "Route Table" inside the application`s "Global.asax" file.
Routing system divided in two part:-
- RouteData: This Technic is used to investigate an incoming URL and then check which controller and action the request is need to be send.
- VirtualPath: This Technic is responsible for generating Outbound URL. These are the URLs that appear in the HTML rendered from our views so that a specific action will be invoked when the user clicks the link.
Sample Code:-
//Default url
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"",
new { controller = "Home", action = "Index", id = "" }
);
//others URL rewriting you want
routes.MapRoute(null,
"Search/{City_State}/{ID}",
new { controller = "Home", action = "Search" }
);
When the routing module finds a exact in the route table for the incoming browser request's URL, it forwards the request to the suitable controller and action.