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

I am trying to create routes that follow the structure of the navigation tree, i.e. i want to include all path in tree in my route. So if I had a tree like this

  • Computers
    • Software
      • Development of
      • Graphic arts
    • Equipment
      • CPU
      • Graphic cards

Then I would like to have routes that look like

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

This in itself is not difficult and can be caught by a route that looks like this

  • directory / {* categories}

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

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

This would mean that I would entrust routes that were defined as the following examples

  • site.com/ linkedin category} / name = {name}
  • site.com/ {* category}

However, the first of these routes is not valid as nothing can appear after a greedy parameter such as {* categories}, so I am a bit stuck. I've been thinking about implementing regex routes, or perhaps using an IRouteContraint to work this way, but I can't think of a decent solution that would allow me to also use Html.ActionLink (...) to generate outgoing URLs that get populated both {* categories} and {name}

Any advice gets very complicated!

Some of you may have seen a similar question yesterday, but this was deleted by me as I have paid more attention to it since then 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

0


source to share


2 answers


Routes ignore query string parameters. But at the same time, query string parameters are passed to the action method until a route URL parameter with the same name is specified. So I would only use the second route and pass the header through the query string.



Another option is more complicated. You write your own route that derives from Route and overrides the GetRouteData method to parse the value of the "categories" (something like RouteData.Values ​​["categories"] and then add the parsed data to the dictionary of route values ​​(RouteData.Values ["title"] = parsedTitle.

+3


source


Greedy segment anywhere in the URL

I wrote a class GreedyRoute

that supports the greedy (catch all) segment anywhere in the URL. It has been a long time since you need it, but it might be useful to others in the future.

It supports any of the following patterns:



  • {segment}/{segment}/{*greedy}

    - this is already supported by default Route

    class
  • {segment}/{*greedy}/{segment}

    - greedy in the middle
  • {*greedy}/{segment}/{segment}

    - greedy at the beginning

You can read all the details in his blog post and get the code.

+4


source







All Articles