4

我有 webapi 和她的方法:

[HttpPost, HttpGet]
[ActionName("GetData")]
public MyData GetData([FromUri] MyData data)
{
  return datamanager.get(data);
}

如何调用此方法?即如何data通过 URL 的查询部分发送参数?

要调用不带参数的 get 方法,我使用以下代码:

public static async Task<myClassl> GetData()
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://sasa.com");
    HttpResponseMessage response = await client.GetAsync("api/GetData");
    myClassl data = await response.Content.ReadAsAsync<myClassl>();

    return data ;
}

谢谢。

4

1 回答 1

1

尝试以下解决方案:

HttpClient client = new HttpClient();
string baseApiAddress = ConfigurationManager.AppSettings["baseApiAddress"];
client.BaseAddress = new Uri(baseApiAddress);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response=client.GetAsync("/api/GetData",data, new JsonMediaTypeFormatter()).Result;
if (response.IsSuccessStatusCode)
{
var mydata = response.Content.ReadAsAsync<MyData>().Result;
}
else
{
Debug.WriteLine(response.ReasonPhrase);
}
于 2013-03-04T21:09:00.270 回答