Sometimes, we need to write a route for catchall
type of url where we can catch any extra data coming in with the url after url segments defined in the route. To do this, we can write in following way.
ROUTECONFIG.CS CODE
routes.MapRoute("CatchAll", "Route/{controller}/{action}/{id}/{*catchall}",
new { controller = "RoutingStuffs", action = "CatchAll", id =
UrlParameter.Optional });
Notice the last segment of the url pattern ({*catchall})In above case, any url starting with “Route” is served by this route. For example
http://localhost:63087/Route/RoutingStuffs//CatchAll/50
Above url is served by this route where Controller is “RoutingStuffs”, action is “CatchAll” (name of the action method) and id is 50 given to the action method.
However, in case the url comes like
http://localhost:63087/Route/RoutingStuffs/CatchAll/50/Delete/And/Other/Parameter
The remaining data after “/50/”, the id segment is given to the controller action method as catchall parameter notice the “{*catchall}” segments defined in the url pattern.
Action method
public ActionResult CatchAll(string id = null, string catchall = null)
{
ViewBag.Action = "Priority: CatchAll";
ViewBag.Controller = "Priority: RoutingStuffs";
ViewBag.Id = id;
ViewBag.CatchAll = catchall;
return View("/Views/RoutingStuffs/OtherStuffs/CatchAll.cshtml");
}
Notice the 2nd parameter of the above action method. "catchall", the name of this parameter should match with the last segment defined in the route {*catchall} . The value of "catchall" parameter would be "/Delete/And/Other/Parameter" in this case.
Views: 24159 | Post Order: 68