# ASP.NET 4 Beta 1: WebForms Routing extensie methoden

<datetime class="hidden">2010-03-14T00:00</datetime>

<!-- category -- mostlylucidcouk, Imported -->
Toen we WebForm routing toevoegden aan ASP.NET 4 Beta 1, hadden we niet de kans om een paar methoden toe te voegen die het werken met Routes en Web Forms een stuk gemakkelijker maken!

Let op: de onderstaande code is incorrect aan die welke wij toevoegen ASP.NET 4 Beta 2, \*dat\* code zal van veel hogere kwaliteit echter, dit is een steekproef alleen!

```
   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:  
```

Hoe gebruik je deze methoden?

Deze extensies voegen een paar eenvoudige methoden toe:

### KaartRoute

Gestolen schaamteloos van MVC. Deze methoden bieden een snelkoppeling naar het definiëren van Routes voor WebForms.

RouteCollection.MapRoute(string naam, string url, string fysicalFile);

Bijvoorbeeld om een eenvoudige route in kaart te brengen die je nu kunt doen:

RouteTable.Routes.MapRoute(...ProductDetailsRoute..., producten/{productid..., ..~/ProductDetails.aspx

Heeft ook een paar varianten waardoor voor het definiëren van defaults etc

RouteCollection.MapRoute(string naam, string url, string fysicalFile, object defaults);

RouteCollection.MapRoute(string naam, string url, string fysicalFile, object defaults, string[] namespaces);

### GetRouteForUrl

Pagina.GetUrlForRoute(string routeNaam, RouteValueDictionary parameters);

Het gebruik van een specifiek genoemde route en RouteValueDictionary met een aantal parameters geeft u een tekenreeks die de Url te gebruiken. ECHT nuttig in databound apps!

b.v.

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

Pagina.GetUrlForRoute(RouteValueDictionary parameters);

Hetzelfde, maar auto-matcht de RouteValueDictionary parameters met een route

### Route negeren

Hiermee kunt u urls die niet moeten worden geprobeerd voor wedstrijden tegen routes te definiëren (kan nuttig zijn voor het upgraden van WebForms apps)

RouteCollection.IgnoreRoute(string url);

RouteCollection.IgnoreRoute(string url, object beperkingen);