I have a method in which I can pass any combination of three parameters, and all of them are nullable. I am using c#, attribute routing, and this is a web API project. If I pass in a value for all three parameters, or if I pass in no parameters all works just fine and dandy. It is the passing of a single parameter whose identity is not inherently clear based on the data that things go sour.
Permit me to explain with an example:
I have the following:
[HttpGet]
[Route("SomePath/{varOne:bool?}/{varTwo:bool?}/{varThree:int?}")]
public async Task<IHttpActionResult> GetItems(bool? one, Bool? two, int? three){...}
As long as I pass in zero or all parameters, all works just fine, it is when I pass in just a single bool value that the trouble begins. You see; the route has absolutely no way to know which bool parameter (either varOne or varTwo) is being passed to it, so it assumes that it is the first one - not terribly conducive to getting correct data.
Help!
After some brief research online, I was educated that the best practice is to have just one SINGLE optional parameter, and the reasoning is more or less along the lines of what I described in the question. Since there is absolutely no way for you to know the difference, keep it to one.
Incidentally, I had no issue when I passed in just the last parameter, and that was due to the fact that it was an int, and it clearly knew which parameter was being passed in.
Here is my code after some revisions, and it now works properly.
[HttpGet]
[Route("SomePath/{varOne:bool?}/{varTwo:bool?}/{varThree:int?}")]
[Route("SomePath/{varThree:int?}")]
[Route("SomePath/second/{varTwo:bool?}")]
public async Task<IHttpActionResult> GetItems(bool? one, Bool? two, int? three){...}
As you can see, I am still using a single method to grab everything, regardless of which parameter will be passed in; however, I did need to resort to using a slightly different URL's within the Route in order to get this going. I did notice that if I had three different data types, I would be able to get away with a single URL.
Since this took me a while to get straight, I felt the need to put it out there. Hope this helps someone.