Asp.net MVC Route class that supports catch-all parameter anywhere in the URL
- by Robert Koritnik
the more I think about it the more I believe it's possible to write a custom route that would consume these URL definitions:
{var1}/{var2}/{var3}
Const/{var1}/{var2}
Const1/{var1}/Const2/{var2}
{var1}/{var2}/Const
as well as having at most one greedy parameter on any position within any of the upper URLs like
{*var1}/{var2}/{var3}
{var1}/{*var2}/{var3}
{var1}/{var2}/{*var3}
There is one important constraint. Routes with greedy segment can't have any optional parts. All of them are mandatory.
Example
This is an exemplary request
http://localhost/Show/Topic/SubTopic/SubSubTopic/123/This-is-an-example
This is URL route definition
{action}/{*topicTree}/{id}/{title}
Algorithm
Parsing request route inside GetRouteData() should work like this:
Split request into segments:
Show
Topic
SubTopic
SubSubTopic
123
This-is-an-example
Process route URL definition starting from the left and assigning single segment values to parameters (or matching request segment values to static route constant segments).
When route segment is defined as greedy, reverse parsing and go to the last segment.
Parse route segments one by one backwards (assigning them request values) until you get to the greedy catch-all one again.
When you reach the greedy one again, join all remaining request segments (in original order) and assign them to the greedy catch-all route parameter.
Questions
As far as I can think of this, it could work. But I would like to know:
Has anyone already written this so I don't have to (because there are other aspects to parsing as well that I didn't mention (constraints, defaults etc.)
Do you see any flaws in this algorithm, because I'm going to have to write it myself if noone has done it so far.
I haven't thought about GetVirtuaPath() method at all.