0

I would like two different methods to be called depending on whether the request is GET /things/123 or GET /things/123?preview

Obviously, this would be a workaround:

[RoputePrefix("api/things")]
...
[HttpGet, Route("{id}")]
public object GetThing(int id)
{
    if (Request.RequestUri.Query == "?preview")
        return GetPreview(id);
    else
        return GetFull(id);
}

...at which point it's almost like using bool preview = false as a parameter and adding ?preview=true to the request. But I want this syntax instead and would prefer to handle it with routing if possible.

For reasons not shown here, I can't do the next easiest thing, which is to use /preview as a suffix.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • You could do something like this: [HttpGet, Route("{id}/preview")] public object GetThing2(int id) It would be GET /things/123/preview, which isn't exactly what you had stated, but probably achieves the same result. – peinearydevelopment Jun 15 '16 at 17:31
  • @peinearydevelopment that adds a path segment, not a querystring. – Diego Mijelshon Jun 15 '16 at 17:33
  • correct, its not clear from your question what you are hoping to achieve. you could do Route("{id}/{param}") public object GetThing(int id, string param) as well, but it is hard to determine from the question what your real goal is. Preview seems like a static value, not a query parameter. – peinearydevelopment Jun 15 '16 at 17:35
  • @peinearydevelopment what isn't clear? I just want to add that flag. The reason why I can't use `/preview` is that `id` is actually a path and contains slashes. I didn't add that to avoid making the question more complicated. – Diego Mijelshon Jun 15 '16 at 17:38

0 Answers0