Using the greedy route parameter in the middle of a route definition

I'm trying to create routes which follow the structure of a tree navigation system, i.e I want to include the entire path in the tree in my route. So if I had a tree which looked like this

  • Computers
    • Software
      • Development
      • Graphics
    • Hardware
      • CPU
      • Graphics cards

Then I would like to be able to have routes that looks like this

  • site.com/catalog/computers/software/graphics

This, on it's own is not hard and can be caught by a route which looks like this

  • catalog/{*categories}

However I want to be able to add the product information at the end of that URL, something like this

  • site.com/catalog/computers/software/graphics/title=Photoshop

Which would mean I would requite routes that were defined like the following examples

  • site.com/{*categories}/title={name}
  • site.com/{*categories}

However the first of these routes are invalid since nothing else can appear after a greedy parameter such as {categories} so I'm a bit stuck. I've been thinking of implementing regex routes or perhaps use IRouteContraint to work my way around this but I can't think of a decent solution that would enable me to also use the Html.ActionLink(...) method to generate outbount URLs which filled in both {categories} and {name}

Any advice is greatly apprechiated!

Some of you may have seen a similar question by me yesterday but that was deleted, by me, since I've since given it more thought and the old question contained incomplete descriptions of my problem

UPDATE 2008/11/26 I posted the solution at http://thecodejunkie.blogspot.com/2008/11/supporting-complex-route-patterns-with.html

Answers


Routes ignore query string parameters. But at the same time, query string parameters are passed in to an action method as long as there isn't a route URL parameter of the same name. So I would use just the second route, and pass in title via the query string.

Another option is more complicated. You write a custom route that derives from Route and override the GetRouteData method so that it parses the value of "categories" (something like RouteData.Values["categories"] and then add the parsed data to the route value dictionary (RouteData.Values["title"] = parsedTitle.


Greedy segment anywhere in the URL

I've written GreedyRoute class that supports greedy (catch all) segment anywhere in the URL. It's been a while since you needed it, but it may be useful to others in the future.

It supports any of the following patterns:

  • {segment}/{segment}/{*greedy} - this is already supported with default Route class
  • {segment}/{*greedy}/{segment} - greedy in the middle
  • {*greedy}/{segment}/{segment} - greedy at the beginning

You can read all the details on my blog post and get the code as well.


Need Your Help

Why is the jQuery code not executing, or at least not appearing to execute, in my jasmine test?

jquery ruby-on-rails jasmine jasmine-jquery

I am running Rails 3.2.8 with the jasmine-rails gem. Here are the lines from my Gemfile describing the jasmine set-up:

Json: parse to display in HTML using JQuery

jquery html asp.net-mvc ajax json

I'm trying to parse data from a page of JSON in my ASP.NET MVC 1.0 web application. I need this data to display in a table on page load and I'm having a great deal of trouble mainly because I've n...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.