Reputation: 60674
I want to map a WebAPI action method to urls on the format api/v2/l8n/{cultureCode}
, but to avoid route collisions with other methods, I need to constrain the cultureCode
parameter to only values matching the regex ^\w{2}(?:-\w{2})?$
, i.e. sv
and en-GB
, but not hello
.
I have a RoutePrefix
attribute on the controller that takes care of api/v2/i8n
, so I tried mapping the action with
[Route(@"{cultureString:regex(^\w{2}(?:-\w{2})?$)")]
but then an error was thrown at configuration time stating that {cultureString:regex(^\w{2
was a bad route value. I tried replacing \w{2}
with \w\w
as well as removing the @
and escaping my backslashes, but was then instead presented with
The route template cannot start with a '/' or '~' character and it cannot contain a '?' character.
If I can't even use ?
, how can I ever create a regex with an optional part?
Upvotes: 0
Views: 1671
Reputation: 59292
I do hope there is no such limitation on the pipeline operator or alternation operator, using which and some anchors, you could get it to work.
[Route(@"{cultureString:regex(^\w\w(-\w{2}|)$)}")]
What the above means is that it matches either two \w
followed by another two \w
or end of the string, $
Upvotes: 1