Answered by:
Variable number of parameters

Question
-
User-867254022 posted
Sorry if this is a dup, tried searching for an existing question but the site continually reported "Site is currently down for maintenance".
Anyway my question is how to specify a route may takes in variable number of parameters and use attribute routing for Web API 2.0?Example, i have a hamburger shop which allows customers to place orders by submitting an order by hitting a URLs. The only requirement, at least 1 ingredient must be specified. After that as many ingredients may be specified and in my real api the set of param values are from a set of 40000 or more items. So for these would be two valid orders requests:
http://www.food.com/api/Hamburger/cheese/pickles/onions/mustard/mayo/lettuce/ketchup/bacon
Or
http://www.food.com/api/Hamburger/mayo/lettuce/ketchupThe best i could find for some sample, but didn't work (assuming c#):
[HttpGet]
[Route( "api/Hamburger/{*options}")]
public void OrderHamburger( ??? ) {...}
Not sure what to use for the method's arguments. At first it felt like i should use "params string[] options", this failed, searching more found RoutingCollection. But stepping into with a debugger the value for options is null. Later i thought how about regex like ".../{options:regex((\w)+)}" -- assuming "(\w)+" is group of words that may occur 1 to 'x' times. No matter what i've attempted debugging in the variable for "options" is always null .
Any suggestions how i can go about this? Thanks in advance.Saturday, May 16, 2015 4:05 AM
Answers
-
User-782957977 posted
As you have n number of options, I would suggest following solution. Please use [FromUri] option
[HttpGet] [Route( "api/Hamburger")] public void OrderHamburger([FromUri]string[] options) { }
http://www.food.com/api/Hamburger?options=cheese&options=pickles&options=onions&options=mustard
Or
Use comma separated options
http://www.food.com/api/Hamburger?options=cheese,pickles,onions,mustard
[HttpGet] [Route( "api/Hamburger")] public void OrderHamburger(string options) { string[] optionsArray = options.Split(','); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, May 17, 2015 10:36 PM
All replies
-
User-782957977 posted
As you have n number of options, I would suggest following solution. Please use [FromUri] option
[HttpGet] [Route( "api/Hamburger")] public void OrderHamburger([FromUri]string[] options) { }
http://www.food.com/api/Hamburger?options=cheese&options=pickles&options=onions&options=mustard
Or
Use comma separated options
http://www.food.com/api/Hamburger?options=cheese,pickles,onions,mustard
[HttpGet] [Route( "api/Hamburger")] public void OrderHamburger(string options) { string[] optionsArray = options.Split(','); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, May 17, 2015 10:36 PM -
User-867254022 posted
Thanks you got me exactly what i needed, also for anyone else out there who might need this and build on it, i experimented a little to see if I really understood this.
Seems for me the comma separated values worked easier as any & will add the values into a separate argument while duplicate "¶m" will add items into the respective array, .
http://www.food.com/api/Hamburger?ingredients=cheese,pickles,onions,mustard?location=Hawaii,Australia,Japan
In code you want:
public OrderHamburger( [FromUri]string[] ingredients,[FromUri]string[] location)
{string[] toppings = ingredients[0].split(...);
string[] locations = location[0].split(...);
And a request like:
http://www.food.com/api/Hamburger?ingredients=cheese,pickles,onions,mustard&location=Hawaii,Australia,Japan&ingredients=tofu
The same web methods appears to work just find but you'll notice ingredients will have 2 items in the array.
string[] toppings = ingredients[0].split(...); // ["cheese","pickles","onions","mustard"];
string[] toppings2 = ingredients[1].split(...); // ["tofu"]
Hope this helps anyone that might need this as well.Monday, May 18, 2015 1:42 AM