I am wondering how people handle nested/complex routes for an application when using a data-driven menu structure?
I’ve got two classes for my data driven menus:
public class List : Entity
{
public virtual ICollection<ListItem> ListItems { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ListType ListType { get; set; }
public string UlTagCssClass { get; set; }
public string UlTagCssID { get; set; }
}
public class ListItem : Entity
{
public virtual List List { get; set; }
public virtual ListItem ParentItem { get; set; }
public virtual ICollection<ListItem> ChildItems { get; set; }
public int SortOrder { get; set; }
public string Text { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public string Url { get; set; }
}
I then have a recursive function that spits out any List into a nested ul tags (which is used as a menu). As it stands right now the ListItem can have a URL via straight up full URL, provide a controller and action, or not be a link at all.
However, I am running into a problem in that the URLs don’t match the hierarchy of the menu, which is pretty common practice and most people would expect.
So for example I have a menu hierarchy such as:
- About
- Contact
- Departments
- Human Resources
- Sales
- Departments
So right now my top level item Contact has .Controller = "Contact". It’s child item Departments has .Controller = "Content", .Action = "Departments". It’s link as you would expect goes to http://www.domain.com/Contact/Departments. But my third level item Sales now has .Controller = "Departments, .Action = "Sales". This makes the link http://www.domain.com/Departments/Sales, which doesn’t make sense in the hierarchy of the menu.
What is a good approach to store the proper URL and generate the routes to support that (I can change the ListItem class if necessary)? Also how would one structure their views/controllers in such a scenario? I am not sure how to continue with the increasing depth as I’ve only ever really developed MVC apps where I hard-coded routes or just used the default controller/action route, but now I am trying to make it more data-driven.
Add the following route and add it before your default routes:
Dynamically map these on application launch by pulling in your menu list and iterating down through the list.
I would prefer not to dynamically map these unless they change on the fly
As you need supporting files (views, etc) that you likely don’t create dynamically