1

I have a basic HttpGet method where people can add an indefinite amount of string params. So basically this means that you can navigate to api/controller/foo, api/controller/foo/bar, api/controller/foo/bar/biz, etc

I tried going at it like below but this doesn't seem to work

[HttpGet("{container}/{prefixes}")]
public async Task<ActionResult<string>> Get(string container, params string[] prefixes)
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
user2657943
  • 2,598
  • 5
  • 27
  • 49

1 Answers1

2

You can use a catch-all template parameter and split the path yourself to solve your problem:

[HttpGet("{container}/{*prefixPath}")]
public async Task<ActionResult<string>> Get(string container, string prefixPath)
{
    string[] prefixes = prefixPath?.Split('/');
    ...
}
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
  • I thought `*` would still encode the forward slashes, while `**` would not? – NotFound Apr 10 '19 at 08:12
  • 1
    I thought so too when reading the documentation. Alas `**` doesn't work and results in an exception. So it's either a bug in the framework or in the documentation. – huysentruitw Apr 10 '19 at 08:30