Webapi get request FromUri list parameter transfer, webapifromuri
When a project calls a third-party webapi today, it finds that the get request uses the FromUri list parameter, Khan...
Here, we will briefly record the request parameter passing method (but we do not recommend that you use the get FromUri list parameter to define the webapi function ):
1 HttpClient hClient = new HttpClient(new MyHttpClienHanlder()); 2 3 string url = "http://api.test.com/api/pk/index?pkId=2322"; 4 //var ret = hClient.GetAsync(url); 5 //var ss = ret.Result.Content.ReadAsStringAsync().Result; 6 //Console.WriteLine(ss); 7 //Console.WriteLine("\r\n\r\n\r\n****************************************************************\r\n\r\n\r\n"); 8 9 List<long> pIds = new List<long>();10 pIds.Add(2322);11 pIds.Add(1824);12 //List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();13 //paramList.Add(new KeyValuePair<string, string>("pkIds", Newtonsoft.Json.JsonConvert.SerializeObject(pIds)));14 15 string parasFormat = "pkIds[{0}]={1}&";16 StringBuilder paras = new StringBuilder();17 for (var i = 0; i < pIds.Count; i++)18 {19 paras.AppendFormat(parasFormat, i, pIds[i]);20 }21 22 url = "http://api.test.com/api/pk/list?" + paras.Remove(paras.ToString().Length - 1, 1).ToString();23 24 //var ret = hClient.PostAsync(url, new FormUrlEncodedContent(paramList));25 var ret = hClient.GetAsync(url);26 var ss = ret.Result.Content.ReadAsStringAsync().Result;27 Console.WriteLine(ss);28 29 hClient.Dispose();