12

I'm using Web API 2 attribute routing and I have a request which is not resolved properly.

[Route("~/foo/{bar?}")]
public void Get(string bar);

My request it's like: mydomain.me/foo/abc/def

I expect to receive bar as "abc/def" but the forward slash messes the route match. Replacing the forward slash with "%2F" doesn't solve the problem.

Dan
  • 1,555
  • 2
  • 14
  • 30

1 Answers1

36

You could use wildcard based matching like below:

[Route("~/foo/{*bar}")]
public string Get(string bar)
Kiran
  • 56,921
  • 15
  • 176
  • 161
  • 1
    Just by way of note: calling it "wildcard based matching" makes it sound like a whole different way of matching. It's not. It's literally just adding a parameter on the end of a route, beginning with a star, to match forward slashes too. – Jez Mar 17 '16 at 09:46
  • also point to be considered while using this approach is that, it will fit if route has only one parameter whose value can contain "/", further that parameter shall be at end in route. but if there are multiple parameters with probability of having "/" in their value or even with single parameter if that can't be put in last of route/url, then also it would not work/fit. – LearningNeverEnds Jul 04 '19 at 09:02
  • Works nicely for me – mts396 Sep 03 '19 at 13:18