C# HttpClient.GetAsync,URI异常无效

C# HttpClient.GetAsync,URI异常无效,c#,asp.net,asp.net-web-api,httpclient,getasync,C#,Asp.net,Asp.net Web Api,Httpclient,Getasync,下面提到的代码不起作用。我在GetAsync中得到以下异常: 无效的URI:无法确定URI的格式 但是,如果我对apiClient.BaseAddress进行注释,并在apiClient.GetAsync中指定完整的URI,那么它可以正常工作 有人能解释一下原因吗 开发环境: VS2012、.NET4.5、C#、ASP.NETWebForms 代码 private async Task WebAPICall() { using (var apiClient = new HttpClien

下面提到的代码不起作用。我在
GetAsync
中得到以下异常:

无效的URI:无法确定URI的格式

但是,如果我对
apiClient.BaseAddress
进行注释,并在
apiClient.GetAsync
中指定完整的URI,那么它可以正常工作

有人能解释一下原因吗

开发环境: VS2012、.NET4.5、C#、ASP.NETWebForms

代码

private async Task WebAPICall()
{
    using (var apiClient = new HttpClient())
    {
        apiClient.BaseAddress = new Uri("http://localhost:9000/");
        apiClient.DefaultRequestHeaders.Accept.Clear();
        apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        apiClient.Timeout = new TimeSpan(0, 0, 30);

        try
        {
            HttpResponseMessage apiResponseMsg = await apiClient.GetAsync(new Uri("api/products/" + txtProductID.Text.Trim()));
            string strResponse = await apiResponseMsg.Content.ReadAsStringAsync();
            Response.Write(strResponse);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    } //using (var apiClient = new HttpClient())
} //private async Task WebAPICall()

在StackOverflow上的另一篇文章的帮助下发现问题。谢谢@Alexei提供的URL

更改了URI字符串,效果良好

HttpResponseMessage apiResponseMsg = await apiClient.GetAsync("api/products/" + txtProductID.Text.Trim());

对于“为什么会发生此错误”,提供是非常重要的。也就是说,在这种情况下,它应该是一行
var uri=new uri(“api/products/”+“productId”)
(我也明白,问题是什么可能非常清楚,但“最小”是很重要的一部分)。如果您的问题更多地是关于使用BaseAddress(如),您可能需要澄清这一点。@AlexeiLevenkov我提供了完整的方法,因为我看到许多人要求它。