We're using RavenDB on the backend and so all the DB keys are strings that contain forward slashes e.g. users/1
.
AFAIK there's no way to use the default ASP NET Web API route matching engine to match a parameter like this, without using a catch-all which means the key must be the last thing on the URL. I tried adding a regex constraint of users/d+
but it didn't seem to make a difference, the route wasn't matched.
What bits would I have to replace to do just enough custom route matching to allow this, preferably without crippling the rest of the route matching. For example, using url: "{*route}"
and a custom constraint that did full regex matching would work but may work unexpectedly with other route configurations.
If your answer comes with some sample code, so much the better.
It seems that it is possible to do this by defining a custom route. In MVC4 (last stable released 4.0.30506.0), it is not possible to do by implementing
IHttpRoute
as per specification but by defining a custom MVC-levelRoute
and adding it directly to theRouteTable
. For details see 1, 2. TheRegexRoute
implementation below is based on the implementation here with mods from the answer here.Define
RegexRoute
:Add this
DelegatingHandler
to avoid aNullReference
due to some other bug:Add handler and route directly to the
RouteTable
:Et voila!
EDIT: This solution has problems when the API is self-hosted (instead of using a WebHost) and requires further work to make it work with both. If this is interesting to anyone, please leave a comment and I'll post my solution.