June 1, 2010 by Christoff Truter C# ASP.NET
URL routing allow developers to decouple/map/rewrite urls into a more readable/descriptive format, also quite handy when
you're busy with migration, SEO etc.
Also see Rudimentary url rewriting
Microsoft introduced routing (.net 3.5 SP1) into its codebase primarily for use with their MVC framework. In
ASP.net 4.0 however Microsoft extended support to Web Forms as well.
It is also possible to implement routing within Web Forms using .net 3.5 SP1, which we will discuss in the next post.
Lets have a quick look at the ASP.net 4.0 Web Forms implementation:
1. In your Application_Start event (within Global.asax), we're going to call a method named MapPageRoutes
and pass the global routing table to the method:
void Application_Start(object sender, EventArgs e) { MapPageRoutes(RouteTable.Routes); }
void RegisterRoutes(RouteCollection routes) { routes.MapPageRoute("testroute", "tester/{id}", "~/test.aspx", false, new RouteValueDictionary { { "id", "1" } }, // default value new RouteValueDictionary { { "id", @"^\d+$" } }); // constraint e.g. only numerals }
public Route MapPageRoute(string routeName, string routeUrl, string physicalFile); public Route MapPageRoute(string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess); public Route MapPageRoute(string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess, RouteValueDictionary defaults); public Route MapPageRoute(string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess, RouteValueDictionary defaults, RouteValueDictionary constraints); public Route MapPageRoute(string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens);
routes.MapPageRoute("testroute", "tester/{id}/{*somename}"...
protected void Page_Load(object sender, EventArgs e) { Response.Write(RouteData.Values["id"]); }
<asp:HyperLink runat="server" ID="HyperLink1" NavigateUrl="<%$RouteUrl:id=20 %>" Text="test"> </asp:HyperLink>
<asp:Label runat="server" ID="Label1" Text="<%$RouteValue:id%>" />
Response.RedirectToRoute("tester", new { id = 20 });
Page.GetRouteUrl("tester", new { id = 20 });
need help January 17, 2012 by abdulghaffar
I'm a beginner and i want to change all my links in my web application by replace ( xxxxx.aspx ) with ( xxxxx.html ) in all the the web application and this is the code i have in mind but i don't know where to start or how? void RegisterRoutes(RouteCollection routes) { routes.MapPageRoute("RootPages", "{file}.html", "~/{file}.aspx"); } void Application_Start(object sender, EventArgs e) { // Code that runs on application startup RegisterRoutes(RouteTable.Routes); } i will be thankful if tell me where to start or how to solve my problem and useful links to read best regard abdulghaffar