I have a Web API method that looks like this:
[Route("datatable")]
public HttpResponseMessage getDataTable([FromUri] decimal a, [FromUri] int p, [FromUri] decimal i, [FromUri] decimal m = 0, [FromUri] decimal e = 0) { ... }
What I'm trying to implement is that any one among i
, m
, or e
must be provided. All can be provided, two of three can be provided, or any single one can be provided, but it is invalid for none to be provided.
As it stands right now, i
is required. I'm currently passing in 0
when i
should be undefined, but this is messy. If I make all three parameters optional, then it is considered valid to provide none of them.
The following calls should be considered valid and should be routed to the method:
http://myhost.local/api/datatable?a=10&p=5&e=1&i=2&m=3
http://myhost.local/api/datatable?a=10&i=1&p=5
http://myhost.local/api/datatable?a=10&i=0&p=5&m=1
http://myhost.local/api/datatable?a=10&p=5&e=1
http://myhost.local/api/datatable?a=10&m=50&p=4
http://myhost.local/api/datatable?e=6&m=2&a=50&p=0
But
http://myhost.local/api/datatable?a=50&p=0
should be invalid and not routed to the method.
I use HTTP attribute routing. I would like for the routing to fail (an API generated 404 "No route matched" message) rather than having to handle all three parameters being undefined in my own code if at all possible. (For example, suppose I do decide later to implement a separate method for the case where no values are provided - I would want the routing to still work.)
Can this be done?