ASP Routing
MSDN, Asp.Net. For debugging, Install-Package routedebugger
In Webforms
See MSDN
In MVC
Routes are defined in from global.asax Application_start: RegisterRoutes(RouteTable.Routes);
Mvc uses the route conventions of {controller} and {action}, with defaults as required.
routes.MapRoute(
"Yearly",
"{year}/{controller}/{action}/{id}",
new { year = DateTime.Now.Year, controller = "Home", action = "IndexByYear", id = UrlParameter.Optional }
);
...will map http://site/1983/Products/display/123 to ProductsController (implementing IController) method Display, passing in parameter with name id=123 and year with default 2010.
Route Defaults and Constraints
routes.MapRoute(
"Yearly",
"{year}/{controller}/{action}/{id}",
new { year = DateTime.Now.Year, controller = "Home", action = "IndexByYear", id = UrlParameter.Optional },
new { year = @"\d{4}" });
Default/ 404 routes
Remember this should always be the last route.
routes.MapRoute("Error",
"{*url}",
new { controller = "Error", action = "404" }
);
In controllers
Controllers have a RedirectToAction() method.
In Views
use Html.ActionLink or Html.RouteLink
<li><a href="/Home/About">Go to About (manual link)</a></li>
<li>@Html.ActionLink("Go to About", "About")</li>
<li>In another controller</li>
<li>@Html.ActionLink("Go to LogOn", "LogOn", "Account")</li>
<li>Below renders as /Home/Details/2 . The final null is important otherwise you get /Home/Details?Length=4</li>
<li>@Html.ActionLink("Go to Details with parameter", "Details", "Home", new { id = 2 }, null)</li>
<li>RouteLinks</li>
<li>@Html.RouteLink("Go to Logon", new { controller = "Account", action = "Logon" })</li>
<li>@Html.RouteLink("Use named route","SpecialRoute")</li>
<li>Areas</li>
<li>@Html.ActionLink("Go to Admin area", "Index", "User", new { area = "Admin" }, null)</li>