【发布时间】:2016-04-06 15:18:05
【问题描述】:
我正在尝试在我的 Web API 应用程序中调用外部托管的 Web API。
在尝试使用 Get () 方法时,一切似乎都正常。
但是,当我尝试实现 Get(int value) 时,我收到了一个错误:
使用路径 'api/External' 和方法 'GET' 的多个操作。
我的控制器出了什么问题?
public class ExternalController : ApiController
{
static string _address = "http://localhost:00000/api/Values";
private string result;
// GET api/values
public async Task<IEnumerable<string>> Get()
{
var result = await GetExternalResponse();
return new string[] { result, "value2" };
}
private async Task<string> GetExternalResponse()
{
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(_address);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync();
return result;
}
public async Task<string> Get(int value)
{
var result = await GetExternalResponse();
return result;
}
}
我也试过下面的方法,似乎也抛出了同样的错误:
private async Task<string> GetExternalResponse2(int value)
{
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(_address + "/" + value);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync();
return result;
}
public async Task<string> Get(int value)
{
var result = await GetExternalResponse2(value);
return result;
}
【问题讨论】:
-
您如何在第二种方法中使用参数值?它没有被传递给外部api吗?
-
是的,你说得对,那是没有被使用的。我现在更新了我也尝试过的另一种方法。
标签: c# .net asp.net-web-api async-await