也许我的问题听起来不同,但我正在尝试做一些独特的事情。 我正在尝试像这样进行API调用:-
public List<TableRecordsEntity> GetTableRecordsList(string ListType, List<string> stringColumnNames, List<string> stringColumnValues, List<int> intValueParams)
{
List<TableRecordsEntity> lstTableRecordsEntity = new List<TableRecordsEntity>();
var baseAddress = "Base";
using (HttpClient client = LocalUtility.InitializeHttpClient(baseAddress))
{
HttpResponseMessage response = client.GetAsync(baseAddress + "/GetTableRecordsList?ListType=" + ListType + "&stringColumnNames=" + stringColumnNames + "&stringColumnValues=" + stringColumnValues + "&intValueParams=" + intValueParams).Result;
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync().Result;
lstTableRecordsEntity = JsonConvert.DeserializeObject<List<TableRecordsEntity>>(data);
}
}
return lstTableRecordsEntity;
}
现在,我不知道如何访问API,因为我无法使其访问API。
[HttpGet]
[Route("api/Base/GetTableRecordsList")]
public IHttpActionResult GetTableRecordsList(string ListType, List<string> stringColumnNames, List<string> stringColumnValues, List<int> intValueParams)
{
return Ok(_repository.GetTableRecordsList(ListType, stringColumnNames, stringColumnValues, intValueParams));
}
我可以做这样的事情还是不能?如果是,那么正确的方法是什么?
答案 0 :(得分:0)
按照惯例,您不会在GET请求中发送数据,所有数据都应通过URL传递。
答案 1 :(得分:0)
您必须在方法的参数中指定[FromUri]。默认情况下,Web API期望它来自Request主体。您可以像这样添加[FromUri]:
public IEnumerable<string> Get([FromUri]List<string> stringColumnNames)
{
return Ok(_repository.GetTableRecordsList(stringColumnNames));
}
添加这样的呼叫
http://localhost:59511/api/Values?str[]="abc"&str[]="xyz"