Search code examples
c#asp.net-mvcasp.net-mvc-routing

Routing an hierarchical path from DB with ASP.NET MVC


Is it possible to route an hierarchical path to map a relation from the database as follows:

Let's say I have an tuple/entity "page" with an m-t-m relation to a "page" (itself) and I want to be able to combine the slug-value of each page to find an appropriate page, like so:

mydomain.com/firstpage/secondpage/thirdpage

where firstpage, secondpage and thirdpage are of type "page" and the third page references to the second page etc.

How would you implement this with ASP.NET MVC routing?


Solution

  • Ok, think I solved it!

    I found out that there is a * (catch-all parameter) that can be used when routing.

    For example:

    routes.MapRoute(
        "Pages",
        "{*pageQuery}",
        new { controller = "Page", action = "GetPage" }
    );
    

    Then in my controller I can use regular expressions or a simple split to resolve each part of the slug. :)