It's not just a 'routing issue' you have on your hands. You need a solution that spans all three tiers:
- Routing and name resolution
- Unique naming and slugifying names
- Storing this in the Database
Each part really is its own separate piece, but your problem will only be solved by solving each of the three.
This is an extremely broad question; To help you further we'd need:
- The table structure OR the class structure and what a sample object pulled back from the database would look like
- The constraints on the names -- is each 'main page' name guaranteed unique? Is every sub-page for a given main page guaranteed unique? Is every sub sub page for a given sub page guaranteed unique? You get the idea.
If your problem is specifically in Routing alone, here's a route that would handle it, and a controller that would handle the route:
routes.MapRoute("Page Resolution",
"{mainPage}/{subPage}/{subSubPage}",
new {controller = "PageController", action = "Page" }
);
routes.MapRoute("Page Resolution",
"{mainPage}/{subPage}",
new {controller = "PageController", action = "Page" }
);
routes.MapRoute("Page Resolution",
"{mainpage}",
new {controller = "PageController", action = "Page" }
);
Controller
public ActionResult Page(string mainPage, string subPage, string subSubPage)
{
var pageRepository = new PageRepository(); //simplified. Don't do this in actual code
var page = pageRepository.GetPage(mainPage, subPage, subSubPage);
var pageViewModel = new PageViewModel(page);
return View(pageViewModel);
}
The code in the repository could handle the overloads to look for a page that doesn't have a sub page or sub sub page.
The code above is simplified because you haven't shown us any code yet that would help me specify. In this case, I've elected to let the repository layer handle what happens if any of those values are String.Empty
; and since I don't know what your data retrieval mechanism is, I can't go further (SQLDataClient, LinqToSql, Entity Framework, NHibernate, etc).
I'm also making the assumption that prettifying and slugifying the URLs are done in accordance with one of the other Stack Overflow questions on the subject.
As a general solution, here are three Stack Overflow questions that solve the different problems you actually have: