mostlylucid

STATIC ARCHIVE of mostlylucid.co.uk of old
posts - 906, comments - 722, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

ASP.NET 4 Beta 1: WebForms Routing extension methods

When we were adding WebForm routing to ASP.NET 4 Beta 1, we didn’t have a chance to add in a couple of methods which make working with Routes and Web Forms a lot easier!

Note: the code below is ‘similar’ to that which we’re adding ASP.NET 4 Beta 2, *that* code will be of much higher quality however, this is a sample only!

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.Routing;
   6:  
   7:     public static class RouteExtensions
   8:     {
   9:         public static string GetUrlForRoute(this System.Web.UI.Page page, string routeName, RouteValueDictionary parameters)
  10:         {
  11:             VirtualPathData vpd= RouteTable.Routes.GetVirtualPath(null, routeName, parameters);
  12:             return vpd.VirtualPath;
  13:         }
  14:         public static string GetUrlForRoute(this System.Web.UI.Page page, RouteValueDictionary parameters)
  15:         {
  16:             VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, parameters);
  17:             return vpd.VirtualPath;
  18:         }
  19:  
  20:         public static void IgnoreRoute(this RouteCollection routes, string url)
  21:         {
  22:             routes.IgnoreRoute(url, null);
  23:         }
  24:  
  25:         public static void IgnoreRoute(this RouteCollection routes, string url, object constraints)
  26:         {
  27:             if (routes == null)
  28:             {
  29:                 throw new ArgumentNullException("routes");
  30:             }
  31:             if (url == null)
  32:             {
  33:                 throw new ArgumentNullException("url");
  34:             }
  35:             IgnoreRouteInternal internal3 = new IgnoreRouteInternal(url);
  36:             internal3.Constraints = new RouteValueDictionary(constraints);
  37:             IgnoreRouteInternal item = internal3;
  38:             routes.Add(item);
  39:         }
  40:  
  41:         public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile)
  42:         {
  43:             return routes.MapRoute(name, url, physicalFile, null, null);
  44:         }
  45:  
  46:         public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile, object defaults)
  47:         {
  48:             return routes.MapRoute(name, url, physicalFile, defaults, null);
  49:         }
  50:  
  51:         public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile, string[] namespaces)
  52:         {
  53:             return routes.MapRoute(name, url, null, null, namespaces);
  54:         }
  55:  
  56:         public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile, object defaults, object constraints)
  57:         {
  58:             return routes.MapRoute(name, url, physicalFile,  defaults, constraints, null);
  59:         }
  60:  
  61:         public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile, object defaults, string[] namespaces)
  62:         {
  63:             return routes.MapRoute(name, url, physicalFile,  defaults, null, namespaces);
  64:         }
  65:  
  66:         public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile, object defaults, object constraints, string[] namespaces)
  67:         {
  68:             if (routes == null)
  69:             {
  70:                 throw new ArgumentNullException("routes");
  71:             }
  72:             if (url == null)
  73:             {
  74:                 throw new ArgumentNullException("url");
  75:             }
  76:             if (physicalFile == null)
  77:             {
  78:                 throw new ArgumentNullException("physicalfile");
  79:             }
  80:             Route route2 = new Route(url, new PageRouteHandler(physicalFile));
  81:             route2.Defaults = new RouteValueDictionary(defaults);
  82:             route2.Constraints = new RouteValueDictionary(constraints);
  83:             Route item = route2;
  84:             if ((namespaces != null) && (namespaces.Length > 0))
  85:             {
  86:                 item.DataTokens = new RouteValueDictionary();
  87:                 item.DataTokens["Namespaces"] = namespaces;
  88:             }
  89:             routes.Add(name, item);
  90:             return item;
  91:         }
  92:  
  93:         // Nested Types
  94:         private sealed class IgnoreRouteInternal : Route
  95:         {
  96:             // Methods
  97:             public IgnoreRouteInternal(string url)
  98:                 : base(url, new StopRoutingHandler())
  99:             {
 100:             }
 101:  
 102:             public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary routeValues)
 103:             {
 104:                 return null;
 105:             }
 106:         }
 107:     }
 108:  

So, how do you use these methods?

These extensions add a few simple methods:

MapRoute

Stolen shamelessly from MVC. These methods provide a shortcut to defining Routes for WebForms.

RouteCollection.MapRoute(string name, string url, string physicalFile);

For example to map a simple route you can now do:

RouteTable.Routes.MapRoute(“ProductDetailsRoute”, “products/{productid}”, “~/ProductDetails.aspx”);

Also has a couple of variants allowing for defining defaults etc…

RouteCollection.MapRoute(string name, string url, string physicalFile, object defaults);

RouteCollection.MapRoute(string name, string url, string physicalFile, object defaults, string[] namespaces);

GetRouteForUrl

Page.GetUrlForRoute(string routeName, RouteValueDictionary parameters);

Using a specifically named route and RouteValueDictionary containing a number of parameters will give you back a string containing the Url to use. REALLY useful in databound apps!

e.g.,

<asp:hyperlink ID="HyperLink1" runat="server"  NavigateUrl='<%# Page.GetUrlForRoute("ProductDetailsRoute",new RouteValueDictionary( new {productid= Eval("ProductID")}))%>'  Text="Details"></asp:hyperlink>

Page.GetUrlForRoute(RouteValueDictionary parameters);

Same thing, but auto-matches the RouteValueDictionary parameters against a route…

IgnoreRoute

Allows you to define urls which should not be tried for matches against routes (can be useful for upgrading WebForms apps)

RouteCollection.IgnoreRoute(string url);

RouteCollection.IgnoreRoute(string url, object constraints);

Print | posted on Tuesday, May 19, 2009 1:23 PM | Filed Under [ Code Snippets ASP.NET 4.0 ]

Feedback

Gravatar

# re: ASP.NET 4 Beta 1: WebForms Routing extension methods

Shouldn't the MapRoute method be called MapPage? What if you want to use your WebForm Routing extensions and MVC in the same app?
5/20/2009 8:29 AM | Haacked
Gravatar

# re: ASP.NET 4 Beta 1: WebForms Routing extension methods

Fine post! I also waiting for the introduction on MVC for VS 2010.
5/20/2009 11:29 PM | Jack

# re: ASP.NET 4 Beta 1: WebForms Routing extension methods

Phil, yes it should be called MapPageRoute...I'll update.
6/30/2009 9:40 PM | Scott Galloway

# re: ASP.NET 4 Beta 1: WebForms Routing extension methods

Has anybody tried using routing with native iis7 output caching. It has a setting for varyByQueryString but since the qs is not changing, a page like /product/1 and /product/2 mapped to product.aspx would return the same thing?
7/13/2009 3:34 AM | download roulette games for free

# re: ASP.NET 4 Beta 1: WebForms Routing extension methods

i have been looking for info on WebForms Routing extension methods, doesnt seem to be much info on them at the minute, thanks
8/16/2009 3:00 PM | asp.net
Comments have been closed on this topic.

Powered by: