Send string array to web api
You can send it in the body or the URL
URL:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="com">// http://localhost:49407/api/values/example6?paramsObject=2,paramsObject=4,paramsObject=9</span> <span class="pun">[</span><span class="typ">Route</span><span class="pun">(</span><span class="str">"example6"</span><span class="pun">)]</span> <span class="pun">[</span><span class="typ">HttpGet</span><span class="pun">]</span> <span class="kwd">public</span> <span class="kwd">string</span> <span class="typ">GetListFromUri</span><span class="pun">([</span><span class="typ">FromUri</span><span class="pun">]</span> <span class="typ">List</span><span class="str"><int></span><span class="pln"> paramsObject</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">paramsObject </span><span class="pun">!=</span> <span class="kwd">null</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="str">"recieved a list with length:"</span> <span class="pun">+</span><span class="pln"> paramsObject</span><span class="pun">.</span><span class="typ">Count</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">return</span> <span class="str">"NOTHING RECIEVED..."</span><span class="pun">;</span> <span class="pun">}</span> |
or in the body:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<span class="pun">[</span><span class="typ">Route</span><span class="pun">(</span><span class="str">"example8"</span><span class="pun">)]</span> <span class="pun">[</span><span class="typ">HttpPost</span><span class="pun">]</span> <span class="kwd">public</span> <span class="kwd">string</span> <span class="typ">GetListFromBody</span><span class="pun">([</span><span class="typ">FromBody</span><span class="pun">]</span> <span class="typ">List</span><span class="pun"><</span><span class="typ">ParamsObject</span><span class="pun">></span><span class="pln"> paramsList</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">paramsList </span><span class="pun">!=</span> <span class="kwd">null</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="str">"recieved a list with length:"</span> <span class="pun">+</span><span class="pln"> paramsList</span><span class="pun">.</span><span class="typ">Count</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">return</span> <span class="str">"NOTHING RECIEVED..."</span><span class="pun">;</span> <span class="pun">}</span> <span class="typ">User</span><span class="pun">-</span><span class="typ">Agent</span><span class="pun">:</span> <span class="typ">Fiddler</span> <span class="typ">Content</span><span class="pun">-</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> application</span><span class="pun">/</span><span class="pln">json </span><span class="typ">Host</span><span class="pun">:</span><span class="pln"> localhost</span><span class="pun">:</span><span class="lit">49407</span> <span class="typ">Content</span><span class="pun">-</span><span class="typ">Length</span><span class="pun">:</span> <span class="lit">91</span> <span class="pun">[{</span><span class="str">"Id1"</span><span class="pun">:</span><span class="lit">3</span><span class="pun">,</span><span class="str">"Id2"</span><span class="pun">:</span><span class="lit">76</span><span class="pun">,</span><span class="str">"Id3"</span><span class="pun">:</span><span class="lit">19</span><span class="pun">},{</span><span class="str">"Id1"</span><span class="pun">:</span><span class="lit">56</span><span class="pun">,</span><span class="str">"Id2"</span><span class="pun">:</span><span class="lit">87</span><span class="pun">,</span><span class="str">"Id3"</span><span class="pun">:</span><span class="lit">94</span><span class="pun">},{</span><span class="str">"Id1"</span><span class="pun">:</span><span class="lit">976</span><span class="pun">,</span><span class="str">"Id2"</span><span class="pun">:</span><span class="lit">345</span><span class="pun">,</span><span class="str">"Id3"</span><span class="pun">:</span><span class="lit">7554</span><span class="pun">}]</span> |
Here’s example of parameters bindings:
http://damienbod.wordpress.com/2014/08/22/web-api-2-exploring-parameter-binding/