June 6, 2010 by Christoff Truter C# ASP.NET
In the previous post we had a quick look at
the routing functionality in ASP.net 4.0.
essentially means (for example) http://somehost/test.aspx?id=1 becomes http://somehost/tester/1
Routing was originally added via .net 3.5 SP1 for use with the Microsoft MVC framework,
but with regards to Web Forms, Microsoft only added some real support in ASP.net 4.0.
It is however possible adding your own support for routing within ASP.net 2.0 & .net 3.5 SP1, lets have a quick
look on how to achieve this.
Step 1: Add a reference to the System.Web.Routing assembly to your project.
Step 2: Make sure that you've got the required entries in your configuration file (e.g. web.config)
<system.web> <httpModules> ... <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </httpModules> </system.web> ... <system.webServer> <modules> ... <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </modules> <handlers> ... <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> </handlers> </system.webServer> ...
using System; using System.Web.Routing; public static class RouteCollectionExtensions { public static Route MapPageRoute(this RouteCollection route, string routeName, string routeUrl, string physicalFile) { return MapPageRoute(route, routeName, routeUrl, physicalFile, null, null, null); } public static Route MapPageRoute(this RouteCollection route, string routeName, string routeUrl, string physicalFile, RouteValueDictionary defaults) { return MapPageRoute(route, routeName, routeUrl, physicalFile, defaults, null, null); } public static Route MapPageRoute(this RouteCollection route, string routeName, string routeUrl, string physicalFile, RouteValueDictionary defaults, RouteValueDictionary constraints) { return MapPageRoute(route, routeName, routeUrl, physicalFile, defaults, constraints, null); } public static Route MapPageRoute(this RouteCollection route, string routeName, string routeUrl, string physicalFile, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens) { if (routeUrl == null) { throw new ArgumentNullException("routeUrl"); } Route item = new Route(routeUrl, defaults, constraints, dataTokens, new RouteHandler(physicalFile)); route.Add(routeName, item); return item; } }
using System.Web; using System.Web.Routing; using System.Web.UI; using System.Web.Compilation; public class RouteHandler : IRouteHandler { private string _physicalFile; public RouteHandler(string physicalFile) { _physicalFile = physicalFile; } public IHttpHandler GetHttpHandler(RequestContext requestContext) { HttpContext.Current.Items["RouteData"] = requestContext.RouteData; return BuildManager.CreateInstanceFromVirtualPath(_physicalFile, typeof(Page)) as Page; } }
void RegisterRoutes(RouteCollection routes) { routes.MapPageRoute("testroute", "tester/{id}", "~/test.aspx", new RouteValueDictionary { { "id", "1" } }, // default value new RouteValueDictionary { { "id", @"^\d+$" } }); // constraint e.g. only numerals } void Application_Start(object sender, EventArgs e) { RegisterRoutes(RouteTable.Routes); }
public System.Web.Routing.RouteData RouteData { get { return HttpContext.Current.Items["RouteData"] as System.Web.Routing.RouteData; } } protected void Page_Load(object sender, EventArgs e) { Response.Write(RouteData.Values["id"]); }
Obvious October 13, 2014 by Christoff Truter
Hi Azami. Obviously I didn't post the whole web.config (hence the ... in my samples), I only posted the relevant parts that generally needs to be added.What your application requires/needs above that is up to you.