title: ASP.NET WebForms GetVirtualPath always returns null
body: <p>I am just getting introduced to ASP.NET routing and have two routes registered at the moment:</p>
  
  <pre><code>        routes.MapPageRoute(
              "default",
              "{Path}/{Name}.aspx{Query}",
              "~/Default.aspx",
              true,
              new RouteValueDictionary { { "Name", "default" } },
              new RouteValueDictionary { { "Name", @"[-_\w]+" } });
  
          routes.MapPageRoute(
              "home",
              "{Name}.aspx{Query}",
              "~/Default.aspx",
              true,
              new RouteValueDictionary { { "Name", "default" } },
              new RouteValueDictionary { { "Name", @"[-_\w]+" } });
  </code></pre>
  
  <p>However, when I try to build urls using these routes, I always get an exception. I tried this:</p>
  
  <pre><code>private string GetVirtualPath(RouteValueDictionary values)
  {
      return RouteTable.Routes.GetVirtualPath(
      null, values).VirtualPath;
  }
  </code></pre>
  
  <p>and also this:</p>
  
  <pre><code>private string GetVirtualPath(RouteValueDictionary values)
  {
      var wrapper = new HttpContextWrapper(HttpContext.Current);
      return RouteTable.Routes.GetVirtualPath(
          new RequestContext(wrapper,
            RouteTable.Routes.GetRouteData(wrapper)), 
                values).VirtualPath;
  }
  </code></pre>
  
  <p>Either way, I get a null reference exception on <code>RouteTable.Routes.GetVirtualPath</code> when I call:</p>
  
  <pre><code>GetVirtualPath(new RouteValueDictionary { { "Name", entity.Name } });
  </code></pre>
  
  <p>Or even:</p>
  
  <pre><code>GetVirtualPath(new RouteValueDictionary { { "Name", entity.Name }, { "Query", string.Empty } });
  </code></pre>
  
  <p>What could I be doing wrong here?</p>