All of the following custom routing rules must not be effective until the default routing rule
Direct method overload + anonymous Object
The most basic routing rule, and also the default routing rule
Routes. Maproute (
Name: "Default",
URL: ' {controller}/{action}/{id} ',
defaults:new {controller = "Home", action = "Index", id = urlparameter.optional}
);
Constructs a route and then adds
Route Myroute = new Route ("{controller}/{action}", New Mvcroutehandler ());
Routes. ADD ("MyRoute0", Myroute);
To add a route to a namespace
In the action, a parameter "catchall" is defined to receive all the parameter values in the URL except the ID
Catchall This will enable the transfer of multiple parameters
Routing across namespaces
Routes. Maproute (
"Addcontollerroute",
"Home/{action}/{id}/{*catchall}",
New {controller = "Home", action = "Index", id = urlparameter.optional},
New[] {"Urlsandroutes.additionalcontrollers"}
);
Routes. Maproute (
"MyRoute1",
' {Controller}/{action}/{id}/{*catchall} ',
New {controller = "Home", action = "Index", id = urlparameter.optional},
New[] {"Urlsandroutes.controllers"}
);
After adding {*catchall} to this configuration after {ID}, you need to increase the string catchall this parameter after the action's parameter ID to get all the parameters except the ID, as follows:
Public ActionResult abctest (string id,string catchall)
{
string temp = ID;
string tt = Catchall;
return Content (ID);
}
Variable-length routing + regular expression matching routing
//Regular expression, in routing configuration, we have made such a rule that only matches the controller name with H
routes. Maproute (
) MyRoute2 ",
" { Controller}/{action}/{id}/{*catchall} ",
New {controller = "Home", action = "Index", id = urlparameter.optional},
New {controller = "^h.*", action = "^index$| ^about$ "},
new[] { "Urlsandroutes.controllers"}
);
//Specify Request methods
routes. Maproute ("MyRoute3", "{Controller}/{action}/{id}/{*catchall}",
New {controller = "Home", action = "Index", id = urlparameter.optional} ,
New {controller = "^h.*", action = "index| About ", HttpMethod = new Httpmethodconstraint (" Get ")},
new[] {"Urlsandroutes.controllers"}
);