title: ASP.NET Routing: How to make routeConfig handle a more dynamic URL structure
body: <p>My scenario is as follows: a venue can be part of multiple categories and users can also add filters on multiple category types, so my URLs now are like:</p>
  
  <ol>
  <li><code>/venues/beaches/boats/themeparks</code> 
  (this will display all venues that are beaches AND boats AND themeparks)</li>
  <li><code>/venues/beaches/boats</code></li>
  <li><code>/venues</code> etc.</li>
  </ol>
  
  <p>So the number of different venue types (beaches, boats, themeparks etc) is both dynamic AND optional.
  I decided to go with URLRouting so I can start to get the different values in codebehind via <code>Request.GetFriendlyUrlSegments()</code></p>
  
  <p>I installed <a href="https://www.nuget.org/packages/Microsoft.AspNet.FriendlyUrls.Core/" rel="nofollow noreferrer">https://www.nuget.org/packages/Microsoft.AspNet.FriendlyUrls.Core/</a> and manually created a <code>RouteConfig.vb</code> file in <code>App_Start</code> folder. This file contains: </p>
  
  <pre><code>Public NotInheritable Class RouteConfig
      Private Sub New()
      End Sub
      Public Shared Sub RegisterRoutes(routes As RouteCollection)
          Dim settings = New FriendlyUrlSettings()
          settings.AutoRedirectMode = RedirectMode.Permanent
          routes.EnableFriendlyUrls(settings)
          routes.MapPageRoute("", "venues", "~/venues.aspx")
      End Sub
  End Class
  </code></pre>
  
  <p>And in <code>global.aspx.vb</code> I added:</p>
  
  <pre><code>Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
      RouteConfig.RegisterRoutes(RouteTable.Routes)
  End Sub
  </code></pre>
  
  <p>I removed ALL rewrite rules from my application, so no rules in <code>web.config</code> or <code>rewriteRules.config</code>.
  When I go to URL: <code>www.example.com/venues</code>, it correctly redirects me to venues.aspx.
  But this URL: <code>www.example.com/venues/boats/outside/castles/barns/restaurants</code>, throws a 404 error:
  <a href="https://i.stack.imgur.com/nVFm5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nVFm5.png" alt="enter image description here"></a></p>
  
  <p>I also checked <a href="https://stackoverflow.com/questions/3523264/asp-net-4-0-url-routing-http-error-404-0-not-found">here</a> and tried that, so enabling http redirection on Windows and changing web.config:</p>
  
  <p><strong>web.config</strong></p>
  
  <pre><code>&lt;!-- tried setting mode to On/Off/Custom --&gt;
  &lt;customErrors mode="Off" defaultRedirect="/error/1" redirectMode="ResponseRedirect"&gt;
    &lt;error statusCode="404" redirect="/404.aspx" /&gt;
  &lt;/customErrors&gt;
  
  &lt;modules runAllManagedModulesForAllRequests="true"&gt;
    &lt;add type="aspnetforum.ForumSEOHttpModule, aspnetforum" name="ForumSEOHttpModule" /&gt;
    &lt;remove name="UrlRoutingModule"/&gt;
    &lt;add name="UrlRoutingModule"
         type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /&gt;
  
  
  &lt;/modules&gt;
  &lt;handlers&gt;
    &lt;remove name="PHP55_via_FastCGI" /&gt;
    &lt;remove name="PHP53_via_FastCGI" /&gt;
    &lt;add name="ScriptCombiner" verb="POST,GET" path="ScriptCombiner.axd" preCondition="integratedMode" type="ScriptCombiner, App_Code" /&gt;
  
    &lt;add name="UrlRoutingHandler"
                preCondition="integratedMode"
                verb="*"
                path="UrlRouting.axd"
                type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/&gt;
  
  
  &lt;/handlers&gt;
  </code></pre>
  
  <p>I found that when I explicitly define the routes in <code>RouteConfig.vb</code>, it does work, like so:</p>
  
  <pre><code>routes.MapPageRoute("", "venues", "~/venues.aspx")
  routes.MapPageRoute("", "venues/boats/outside/castles/barns/restaurants", "~/venues.aspx")
  </code></pre>
  
  <p>So now URL <code>/venues</code> and <code>/venues/boats/outside/castles/barns/restaurants</code> work, but that is not dynamic at all. Since there are a a lot of venue categories and combinations, I don't want to add all these combinations manually. I just want to match on all URLs that start with <code>/venues</code> and then use method <code>Request.GetFriendlyUrlSegments</code> in <code>venues.aspx.vb</code> to get the venue categories.</p>
  
  <p>How can I do this?</p>