title: Dynamic Routes from database for ASP.NET MVC CMS
body: <p>Basically I have a CMS backend I built using ASP.NET MVC and now I'm moving on to the frontend site and need to be able to load pages from my CMS database, based on the route entered.</p>
  <p>So if the user enters <code>example.com/students/information</code>, MVC would look in the pages table to see if a page exists that has a permalink that matches students/information, if so it would redirect to the page controller and then load the page data from the database and return it to the view for display.</p>
  <p>So far I have tried to have a catch all route, but it only works for two URL segments, so <code>/students/information</code>, but not <code>/students/information/fall</code>. I can't find anything online on how to accomplish this, so I though I would ask here, before I find and open source ASP.NET MVC CMS and dissect the code.</p>
  <p>Here is the route configuration I have so far, but I feel there is a better way to do this.</p>
  <pre><code> public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute(&quot;{resource}.axd/{*pathInfo}&quot;);
  
          // Default route to handle core pages
          routes.MapRoute(null,&quot;{controller}/{action}/{id}&quot;,
                          new { action = &quot;Index&quot;, id = UrlParameter.Optional },
                          new { controller = &quot;Index&quot; }
          );
  
          // CMS route to handle routing to the PageController to check the database for the route.
  
          var db = new MvcCMS.Models.MvcCMSContext();
          //var page = db.CMSPages.Where(p =&gt; p.Permalink == )
          routes.MapRoute(
              null,
              &quot;{*.}&quot;,
              new { controller = &quot;Page&quot;, action = &quot;Index&quot; }
          );
      }
  </code></pre>
  <p>If anybody can point me in the right direction on how I would go about loading CMS pages from the database, with up to three URL segments, and still be able to load core pages, that have a controller and action predefined.</p>