1

I am working on an E-Commerce website and my requirement is to show URLs something like following. "../example.com/Electronics/Home-Audio-and-Theater/Portable-Audio/iPods"

Where Electronics is Main category and all of the subsequent parts are subcategories.

I have a Menu which shows level upto iPods(3rd level). I want that on click of any level the URL should be created accordingly. For Example:-

If I click on Electronics the URL should be:-
../example.com/Electronics
If clicked on "Home-Audio-and-Theater"
example.com/Electronics/Home-Audio-and-Theater
and so on.

All of the Names of levels are dynamic so I can't create controller for each level. I need a technique by which I can show these URLs according to menu item clicked and hit the controller. Please suggest ideas for this.
Thank You

  • Have a look at [this answer](http://stackoverflow.com/questions/31934144/multiple-levels-in-mvc-custom-routing/31958586#31958586). You can create a custom `RouteBase` subclass and have your URLs look any way you want. I would suggest making a self-joined table to build the URL segments and then only have a single controller action that handles the display. – NightOwl888 Aug 13 '15 at 14:13
  • I have taken a look at that but I can't figure out how to use this in my scenario. I have a table from where these categories and subcategories are coming. And the client doesn't want any static name of controller and action. He just wants to start with his top level category as suggested in the question. I am not that much experienced in MVC so sorry for not getting your advance solution.:-) – Manvendra Singh Aug 14 '15 at 05:26
  • @NightOwl888. Can this be done with attribute routing? I don't have very clear idea about that too but I think the URL with parameter can be changed to a more "hackable" one with attribute routing. Am I right? – Manvendra Singh Aug 14 '15 at 06:16
  • A custom `RouteBase` is the most flexible solution, but also the most complex to implement. Depending on your requirements, attribute routing may work - it really depends on how dynamic your categories and content pages need to be. For example, you would probably not want to make a controller action for every content item (product) and you would probably want the content item to include the category in the URL (`/Electronics/Home-Audio-and-Theater/Sony-42-Inch-LCD-TV`), which would be more reasonable to do with a custom `RouteBase`. – NightOwl888 Aug 14 '15 at 09:20
  • BTW - Learning what routing does at the lowest level (`RouteBase`) is the best way to understand what the more abstract `MapRoute`, `MapPageRoute`, and `RouteAttribute` are really doing under the covers. – NightOwl888 Aug 14 '15 at 09:25

0 Answers0